I am having big problem understanding how namespace works in the RequestURI of Apex Rest Web Services when you deploy them as part of a managed package, and hence, writing a proper Test Class for that, which will run in dev org, packaging org and subscriber org as well.
This is my APEX Rest WS:
@RestResource(urlMapping='/SyncSFBusinessPartner/*')
global with sharing class BusinessPartnerSyncService {
@HttpPatch
global static void updateBusinessPartner() {
//some processing
}
}
My dev org has a namespace bpgdev, and this is what I have written as part of the Test Class
RestRequest restRequest = new RestRequest();
RestResponse restResponse = new RestResponse();
restRequest.requestURI = System.Url.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/bpgdev/SyncSFBusinessPartner';
//added bpgdev in line above as that is ns of dev org
restRequest.httpMethod = 'PATCH';
restRequest.requestBody = Blob.valueOf(gen.getAsString());//gen is a JSONGenerator
RestContext.request = restRequest;
RestContext.response = restResponse;
BusinessPartnerSyncService.updateBusinessPartner();
Blob responseBody = RestContext.response.responseBody;
System.assert(responseBody != null);
for (Account account: [SELECT Name FROM Account]) {
System.assert(account.Name.startsWith('TestAccount') == false);
System.assert(account.Name.startsWith('partnerName') == true);
} // all asserts work, web service updates data successfully
I have deployed the whole code to my packaging org where namespace is different (bpgpck), and I totally expected the test class to fail as my dev org namespace is still hardcoded in my test class. I know of the solution here suggested by @DanielBallinger to query for namespace, but that is not my question.
My question is, the whole Test Class succeeds and the data update assertions are all correctly running in the packaging org as well.
So can someone kindly explain, when a test class invokes an Apex Rest web service, does it ignore the namespace in the requestURI altogether?
Sorry if I sound confused, because I am..