当我尝试使用https而不是http时,为什么我的单元测试会失败我的顶级课程

时间:2018-03-22 08:00:26

标签: https salesforce apex

我遇到的问题是,当我尝试从http切换到https时,我的单元测试失败,我无法将代码推送到生产环境。

我试图将一些数据从apex类推送到另一个框架。由于此数据可能有点敏感,我希望使用salesforce提供的客户端证书选项对其进行加密。



public class someclaass { 
    @future(callout=true) 
    public static void SendData(String id, String name, String token) { 
        HttpRequest req = new HttpRequest(); 
        req.setEndpoint('https://someurl.com/');
        req.setMethod('POST');
        req.setHeader('content-type', 'application/json');
        req.setClientCertificateName('certificatename');
        req.setBody('{"id" : "' + id + '", "name" : "' + name + '", "token" : "' + token + '"}');
        Http http = new Http();
        HttpResponse res = http.send(req);
    }
    
}






@istest
global class Some_mock implements HttpCalloutMock {
    
    /* This method fakes a reponse to our HTTP request to Blue10 */
    global HTTPResponse respond(HTTPRequest req) {
        
        String endpoint = req.getEndpoint();
        system.debug(endpoint);
        System.assert(endpoint.startsWith('https://someurl.com'));
        String responseBody = '';
        String destination = '';
        
        // get the actual API function we're calling
        String[] parts = endpoint.split('/');
        destination = parts[parts.size()-1];
        
        system.debug(destination);
        
        if(destination == 'sendData') {
            System.assertEquals('POST', req.getMethod());
            responseBody = Some_mock.SomeMock();
        } else {
            System.debug(LoggingLevel.ERROR, 'We don\'t have a test case for this API function yet: '+destination);
            //System.assert(true == false);
        }
        
        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        res.setBody(responseBody);
        res.setStatusCode(200);
        return res;
    }  
    
    public static String SomeMock() {
        String body = 
            ' {'+
            '   "status": "success",'+
            '   "message": null,'+
            '   "code": 200'+
            ' }';
        return body;
    }
}






@isTest
private class SomeTest { 
    static testMethod void validateSomeClass() { 
    
        Test.setMock(HttpCalloutMock.class, new Some_mock());
        
        Test.startTest();
        
        Some.Senddata('id', 'name', 'token');
        List<SomeInvocable.SomeVariable> SomeList = new List<SomeInvocable.SomeVariable>();
        SomeInvocable.SomeVariable orobj = new SomeInvocable.SomeVariable();
        orobj.id = 'id';
        orobj.name = 'name';
        orobj.token = 'token';
        SomeList.add(orobj);
        SomeInvocable.sendMessage(SomeList);
        
        Test.stopTest();
        
    }
    
}
&#13;
&#13;
&#13;

以上是我在两个网址而不是https://中设置http://的时刻,但我不知道我在这里做错了什么。

0 个答案:

没有答案