function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
skiershortyskiershorty 

How to test a method that uses ApexPages.currentPage().getHeaders().get('Referer')

When a Visualforce Page1 calls Visualforce Page2 and Visualforce Page2 has a extension to an Apex class with a constructor that contains the following line of code

 

"ApexPages.currentPage().getHeaders().get('Referer')"

 

How can the class be tested when being called by a Apex test class instead of a Visualforce page?

 

The result of "ApexPages.currentPage().getHeaders().get('Referer')" is setting a class property that is being used by other methods of the class.

 

//*****************************************************

public class Payment_Extension
{
    private Payment__c payment;
    private ApexPages.StandardSetController myController;
    private Payment currentPayment;
    private boolean isReceivable;    
    
    //********************************************************************************
    public Payment_Extension(ApexPages.StandardSetController stdController)
    {
        system.debug('PaymentReceivable_Extension');
        payment = (Payment__c)stdController.getRecord();
        myController = stdController;
        isReceivable = ApexPages.currentPage().getHeaders().get('Referer').contains('tabspayreceivable');     
    }
    
    //********************************************************************************
    public List<selectOption> getReceivableTypes()
    {
        if(isReceivable)
        {
            return currentPayment.ReceivableTypes();
        }
        else
        {
            return currentPayment.PayableTypes();
        }
    }
}

//*****************************************************

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You should just be able to put a value into the headers map:

 

ApexPages.currentPage().getHeaders().put('referer', 'test');

 

All Answers

bob_buzzardbob_buzzard

You should just be able to put a value into the headers map:

 

ApexPages.currentPage().getHeaders().put('referer', 'test');

 

This was selected as the best answer
jhennyjhenny

Any idea why I can't set a header in a pagereference that can be captured by another page, i.e.: 

 

public PageReference redirect(){ 

   Apexpages.Pagereference kbhome = new Apexpages.pagereference('/apex/refererredirect');

   kbhome.getHeaders().put('SomeHeader','SomeValue');

   kbhome.setRedirect(true);

   return kbhome;

}

 

The header doesn't show up in the other VF page.

 

Thanks

 

 

SFDC-SDSFDC-SD

Hi

 

Can you please give a example on how and where to use setHeader method?

 

I tried the same stuff using getHeaders but I am always getting the error on the below link:

 

http://boards.developerforce.com/t5/Apex-Code-Development/Cyclical-server-side-forwards-detected-Did-anyone-face-this-APEX/td-p/504167

 

Any help on this is appreciated!