$ http是否对params对象进行排序?

时间:2015-04-14 08:31:36

标签: javascript angularjs rest unit-testing http

我为angularjs应用程序编写了一些单元测试。对于测试,我使用内部$ httpBackend模拟$ http请求。

在测试期间,我使用$ httpBackend.expectGET,因为我想要我的应用程序请求的确切行为。

例如,我有一个Params-Object:

parameters = {
        name : 'Monkey',
        crazy : false,
        desc : 'Nobody',
      };

我的应用中的Http-Get请求是:

return $http.get(this.uri + '/' + id, {params : parameters});

在我的单元测试中,我希望如此:

$httpBackend.expectGET(instance.uri + '/' + returnValues.id + '?' + query).respond(200, object);

"查询"只是对象的元素与' ='和''。 所以我期待这个网址:

 www.example.com/api/v1/object/1?name=Monkey&crazy=false&desc=Nobody

但我得到了这个:

www.example.com/api/v1/object/1?crazy=false&desc=Nobody&name=Monkey

$ http是否根据" params" 的对象中的键进行排序?

1 个答案:

答案 0 :(得分:2)

是的,$http根据source code

将参数发送到服务器之前对参数进行排序
forEachSorted(params, function(value, key) {
    ...
});

因此,您的测试应该是有序的密钥,或者您可以通过将其发送给$http提供商来编写您自己的paramSerializer:

  

paramSerializer - {string | function(Object):string} - 使用的函数   准备请求参数的字符串表示(指定为   一个东西)。指定为字符串,它被解释为函数   使用{$ injector}注册。

相关问题