Sinon fakeServer无法模拟POST:'TypeError:通过RFC7230,第3.2.4节,标头值应为字符串。有物体

时间:2019-02-21 04:30:22

标签: mocking sinon mockserver

GET方法可以正常工作,但始终会遇到POST问题。 我的测试代码是

let server = sinon.createFakeServer();
server.respondImmediately = true;
server.respondWith(`api/v1.0/companies/${companyResponse.id}/orgchart`, [200,  {"Content-Type": "*/*"}, 'OK']);

和下面是ajax调用:

  ajax({
    type: "POST",
    url: '/my/api/endpoint',
    data: data,
    processData: false,
    contentType: false,
    complete: this.handleLoadingComplete,
    xhr: function(){
      let xhr = $.ajaxSettings.xhr() ;
      xhr.upload.onprogress = function(evt){
        updateProgress(evt.loaded, evt.total);
      } ;
      xhr.upload.onload = function(){ console.log('loading completed') } ;
      return xhr ;
    }        
  });

使用完全相同的测试代码,当我将ajax方法设置为“ GET”时,它可以工作。但是使用“ POST”总是失败。这是complete对上述ajax POST的响应:

`

{ readyState: 0,
      getResponseHeader: [Function: getResponseHeader],
      getAllResponseHeaders: [Function: getAllResponseHeaders],
      setRequestHeader: [Function: setRequestHeader],
      overrideMimeType: [Function: overrideMimeType],
      statusCode: [Function: statusCode],
      abort: [Function: abort],
      state: [Function: state],
      always: [Function: always],
      catch: [Function: catch],
      pipe: [Function: pipe],
      then: [Function: then],
      promise: [Function: promise],
      progress: [Function: add],
      done: [Function: add],
      fail: [Function: add],
      status: 0,
      statusText:
       'TypeError: By RFC7230, section 3.2.4, header values should be strings. Got object' }

` 我不知道这里出了什么错误,我被困了几个小时....有人可以帮忙吗?将不胜感激!

1 个答案:

答案 0 :(得分:1)

我有同样的问题,就我而言,这与axios如何设置标头有关。这不是真正的解决方案,但是固定sinon并强制使用nise的旧版本可以解决此问题。这是一个package.json代码段:

  ...
  "devSependencies": {
    ...
    "sinon": "^4.5.0",
    ...
  }
  ...
  "resolutions:" {
    "nise": "1.2.7"
  }
  ...