Hi We have a apex class and trying to write Apex class for the code coverage. For some reason the Test class wont pass.
Please advise on this Iam getting Attempt to de reference error.
Please find the code below.
public class headerUtility {
/** This is a hack to determine is the page is using SSL or not. If CipherSuite is defined that means it is HTTPS **/
public Boolean hasSSL { get { return ApexPages.currentPage().getHeaders().get('CipherSuite') != null; } }
/** This is used to determine the device type **/
public String userAgent { get { return ApexPages.currentPage().getHeaders().get('USER-AGENT'); } }
/** This will return which Salesforce Server you are on, example: na8.salesforce.com **/
public String currentServer { get {return ApexPages.currentPage().getHeaders().get('X-Salesforce-Forwarded-To'); } }
/** Check the useragent string and assign the device **/
public String deviceType { get; set; } {
if(userAgent.contains('iPhone')) deviceType = 'iPhone';
else if(userAgent.contains('iPad')) deviceType = 'iPad';
else if(userAgent.contains('BlackBerry')) deviceType = 'BlackBerry';
else deviceType = 'Other';
}
/** You could query a custom object to determine your home page, check the device or simply hard code it here **/
public String returnUrl {get; set; } {
if (deviceType == 'iPad') {
returnUrl = '/apex/LbMain';
} else {
returnUrl = '/apex/LbMain';
}
}
public String username {get; set;}
public String password {get; set;}
public PageReference login(){
String startUrl = System.currentPageReference().getParameters().get('startURL');
return Site.login( username, password, startURL);
}
/** Check to see if the page is using HTTPS, if not redirect it back to itself with HTTPS **/
public PageReference redirect() {
if (!hasSSL) {
string host = ApexPages.currentPage().getHeaders().get('host');
string url = ApexPages.currentPage().getUrl();
PageReference homePage = new PageReference('https://'+host+url);
homePage.setRedirect(true);
return homePage;
} else {
return null;
}
}
}
Test class:
@istest
public class HeaderUtility_Test {
public static testmethod void TestHeader(){
headerUtility h = new headerUtility();
h.username = 'Test@hilton.com';
h.password ='Test123!';
pagereference pageref = page.LbMain;
test.setCurrentPage(pageref);
h.login();
h.redirect();
}
}
Error Message:
System.NullPointerException: Attempt to de-reference a null object
Class.autoFinance.headerUtility.: line 14, column 1
Class.autoFinance.HeaderUtility_Test.TestHeader: line 6, column 1.
Thank You