Issue with URL Parameters not decoding during Test Methods

Typing the URL into the browser with the following params:

rec=atWAzwsBvm3hyWrApj4mBo0BsaasxAmfySPTrXZxNVbdPIMHXr1FfVg2Fhoa%2B7FY

NOT During a test method executing the line:

system.debug(logginglevel.error,ApexPages.currentPage().getParameters().get('rec'));

Debugs the following value:

atWAzwsBvm3hyWrApj4mBo0BsaasxAmfySPTrXZxNVbdPIMHXr1FfVg2Fhoa+7FY

However the following code DURING A Test Method

PageReference pr = Page.Respond_Invite;    pr.getParameters().put('rec','atWAzwsBvm3hyWrApj4mBo0BsaasxAmfySPTrXZxNVbdPIMHXr1FfVg2Fhoa%2B7FY');
test.setCurrentPage(pr);

Debugs the following:

atWAzwsBvm3hyWrApj4mBo0BsaasxAmfySPTrXZxNVbdPIMHXr1FfVg2Fhoa%2B7FY

Any ideas why the getParameters decodes the URL Param when a test method is not running and does not decode it when a test method is running?

In order to successfully execute the test I have to add do this:

pr.getParameters().put('rec',EncodingUtil.urlDecode('atWAzwsBvm3hyWrApj4mBo0BsaasxAmfySPTrXZxNVbdPIMHXr1FfVg2Fhoa%2B7FY','UTF-8'));

There is no code in-between just the debug…

Answer

The decoding of URL parameter

rec=atWAzwsBvm3hyWrApj4mBo0BsaasxAmfySPTrXZxNVbdPIMHXr1FfVg2Fhoa%2B7FY

is done by the browser in which the request is run and the decoded param is passed to the underlying controller(in our case its apex controller).

But in the case of test, when you set the URL parameter, there won’t be any browser to decode that parameter and send it to its underlying controller. So the apex controller receives the URL parameter as such available.

So its the developer’s responsibility to test with proper URL encoding and decoding.

Hope it helps.

Attribution
Source : Link , Question Author : Eric , Answer Author : Vigneshwaran G

Leave a Comment