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
BryanTSCBryanTSC 

URL Rewriter and Test Class

A couple of questions here:

 

1)  Does this URL Rewriter class look functional?  I thought I read that I didn't necessarily need the second generateURL function, but I'm not sure.

 

 

global with sharing class myRewriter implements Site.UrlRewriter {

String VIP_PAGE = '/VIP/';
String VIP_VISUALFORCE_PAGE = '/VIP?id=';

global PageReference mapRequestUrl(PageReference
myFriendlyUrl){
String url = myFriendlyUrl.getUrl();
if(url.startsWith(VIP_PAGE)){
String name = url.substring(VIP_PAGE.length(),
url.length());

Lead ld = [select id from Lead where XUniqueSearchId__c =:
name LIMIT 1];

return new PageReference(VIP_VISUALFORCE_PAGE + ld.id);
}

return null;
}

global List<PageReference> generateUrlFor(List<PageReference>
mySalesforceUrls){

return null;
}

}

 

 

2) What should the test class for this consist of?

 

Thank you in advance for any assistance you may be able to provide!

Best Answer chosen by Admin (Salesforce Developers) 
Ryan-GuestRyan-Guest

Yes, your class looks functional.

 

Here is what your example looks like with some tests:

 

 

global with sharing class myRewriter implements Site.UrlRewriter {

    String VIP_PAGE = '/VIP/';
    String VIP_VISUALFORCE_PAGE = '/VIP?id=';

    global PageReference mapRequestUrl(PageReference myFriendlyUrl){
        String url = myFriendlyUrl.getUrl();
        if(url.startsWith(VIP_PAGE)){
            String name = url.substring(VIP_PAGE.length(), url.length());
    
            Lead ld = [select id from Lead where XUniqueSearchId__c =: name LIMIT 1];
            return new PageReference(VIP_VISUALFORCE_PAGE + ld.id);
        }
        return null;
    }

    global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls){
        return null;
    }
    
    @isTest
    static void testGenerateUrlFor() {
        myRewriter rewriter = new myRewriter();
        System.assert(rewriter.generateUrlFor(null) == null);
    }
    
    @isTest
    static void testMapUrl() {
        Lead testLead = new Lead(XUniqueSearchId__c = 'testId', lastName='Guest', Company='Salesforce');
        Id testId = Database.insert(testLead).id;
        
        
        myRewriter rewriter = new myRewriter();
        String actualResult = rewriter.mapRequestUrl(new PageReference('/VIP/testId')).getUrl();
        
        String expectedResult = '/VIP?id=' + testId;
        //System.debug(actualResult);
        System.assert(actualResult == expectedResult);
    }
    
    @isTest
    static void testMapNonVipUrl() {

        
        
        myRewriter rewriter = new myRewriter();
        PageReference actualResult = rewriter.mapRequestUrl(new PageReference('/testUrl'));
        
        String expectedResult = '/testUrl';
        System.assert(actualResult == null);
    }

}

 

 

All Answers

Ryan-GuestRyan-Guest

Yes, your class looks functional.

 

Here is what your example looks like with some tests:

 

 

global with sharing class myRewriter implements Site.UrlRewriter {

    String VIP_PAGE = '/VIP/';
    String VIP_VISUALFORCE_PAGE = '/VIP?id=';

    global PageReference mapRequestUrl(PageReference myFriendlyUrl){
        String url = myFriendlyUrl.getUrl();
        if(url.startsWith(VIP_PAGE)){
            String name = url.substring(VIP_PAGE.length(), url.length());
    
            Lead ld = [select id from Lead where XUniqueSearchId__c =: name LIMIT 1];
            return new PageReference(VIP_VISUALFORCE_PAGE + ld.id);
        }
        return null;
    }

    global List<PageReference> generateUrlFor(List<PageReference> mySalesforceUrls){
        return null;
    }
    
    @isTest
    static void testGenerateUrlFor() {
        myRewriter rewriter = new myRewriter();
        System.assert(rewriter.generateUrlFor(null) == null);
    }
    
    @isTest
    static void testMapUrl() {
        Lead testLead = new Lead(XUniqueSearchId__c = 'testId', lastName='Guest', Company='Salesforce');
        Id testId = Database.insert(testLead).id;
        
        
        myRewriter rewriter = new myRewriter();
        String actualResult = rewriter.mapRequestUrl(new PageReference('/VIP/testId')).getUrl();
        
        String expectedResult = '/VIP?id=' + testId;
        //System.debug(actualResult);
        System.assert(actualResult == expectedResult);
    }
    
    @isTest
    static void testMapNonVipUrl() {

        
        
        myRewriter rewriter = new myRewriter();
        PageReference actualResult = rewriter.mapRequestUrl(new PageReference('/testUrl'));
        
        String expectedResult = '/testUrl';
        System.assert(actualResult == null);
    }

}

 

 

This was selected as the best answer
Ryan-GuestRyan-Guest

Also, unless you are using the UrlFor function in your pages and you want to change those URLs, you don't need to use the generateUrlFor method.

BryanTSCBryanTSC

Thank you for your quick help with this.

 

I've run some tests and everything appears to be checking out, but when I try to use the friendlier URL I get a "Page Not Found" message.  I've added the Rewriter class to the site settings and also to the enabled apex classes, so I'm not sure what's happening. 

 

Am I missing something here?!

Ryan-GuestRyan-Guest

Is the VIP page added to your site as well?

BryanTSCBryanTSC

I figured it out . . . doh!

 

VIP was the site, so really the link would have to be /VIP/VIP?id= in order for it to work.

 

Thanks!

ShawnFShawnF

Can you tell me what HTTP response your OLD urls are sending? 200? 301? 404?

 

I am interested because I have a Force.com Site where many URLs are already indexed by Google. If I apply this URL rewrite class to my Force.com Site, I want to ensure that the OLD urls are givng the index spiders a valid 301 redirect HTTP response.

Martin ProkopMartin Prokop
Hello, I have the same question. Is it 301 redirect?