nock-获取404响应

时间:2019-03-24 17:42:31

标签: nock

我已经设置了一个nock拦截器,但是得到了404响应:

{ Error: Request failed with status code 404
        at createError (/Users/joe/Sources/tidee-life/packages/tidee-life-web/node_modules/axios/lib/core/createError.js:15:15)
        at settle (/Users/joe/Sources/tidee-life/packages/tidee-life-web/node_modules/axios/lib/core/settle.js:18:12)
        at IncomingMessage.handleStreamEnd (/Users/joe/Sources/tidee-life/packages/tidee-life-web/node_modules/axios/lib/adapters/http.js:186:11)
        at emitNone (events.js:111:20)
        at IncomingMessage.emit (events.js:208:7)
        at endReadableNT (_stream_readable.js:1064:12)
        at _combinedTickCallback (internal/process/next_tick.js:138:11)
        at process._tickCallback (internal/process/next_tick.js:180:9)
      config: 
       { adapter: [Function: httpAdapter],
         transformRequest: { '0': [Function: transformRequest] },
         transformResponse: { '0': [Function: transformResponse] },
         timeout: 30000,
         xsrfCookieName: 'XSRF-TOKEN',
         xsrfHeaderName: 'X-XSRF-TOKEN',
         maxContentLength: -1,
         validateStatus: [Function: validateStatus],
         headers: 
          { Accept: 'application/json, text/plain, */*',
            'Content-Type': 'application/json; charset=UTF-8',
            Authorization: 'bearer null',
            'User-Agent': 'axios/0.15.3',
            'Content-Length': 45 },
         host: 'http://example.com',
         method: 'post',
         url: 'http://example.com/auth/login',
         data: '{"email":"test@test.com","password":"qwerty"}',
         responseType: 'json' },
      response: 
       { status: 404,
         statusText: 'Not Found',
         headers: 
          { 'cache-control': 'max-age=604800',
            'content-type': 'text/html; charset=UTF-8',
            date: 'Sun, 24 Mar 2019 17:36:01 GMT',
            expires: 'Sun, 31 Mar 2019 17:36:01 GMT',
            server: 'EOS (vny006/0452)',
            'content-length': '345',
            connection: 'close' },
         config: 
          { adapter: [Function: httpAdapter],
            transformRequest: [Object],
            transformResponse: [Object],
            timeout: 30000,
            xsrfCookieName: 'XSRF-TOKEN',
            xsrfHeaderName: 'X-XSRF-TOKEN',
            maxContentLength: -1,
            validateStatus: [Function: validateStatus],
            headers: [Object],
            host: 'http://example.com',
            method: 'post',
            url: 'http://example.com/auth/login',
            data: '{"email":"test@test.com","password":"qwerty"}',
            responseType: 'json' },
         request: 
          Writable {
            _writableState: [Object],
            writable: true,
            domain: null,
            _events: [Object],
            _eventsCount: 2,
            _maxListeners: undefined,
            _options: [Object],
            _redirectCount: 0,
            _onNativeResponse: [Function],
            _currentRequest: [Object],
            _currentUrl: 'http://example.com' },
         data: '<?xml version="1.0" encoding="iso-8859-1"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n\t<head>\n\t\t<title>404 - Not Found</title>\n\t</head>\n\t<body>\n\t\t<h1>404 - Not Found</h1>\n\t</body>\n</html>\n' } }

下面是记录的nock呼叫:

 {
      "scope": "http://example.com:80",
      "method": "post",
      "path": "/auth/login",
      "body": {
        "email": "test@test.com",
        "password": "qwerty"
      },
      "status": 404,
      "response": "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n         \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n\t<head>\n\t\t<title>404 - Not Found</title>\n\t</head>\n\t<body>\n\t\t<h1>404 - Not Found</h1>\n\t</body>\n</html>\n",
      "rawHeaders": [
        "Cache-Control",
        "max-age=604800",
        "Content-Type",
        "text/html; charset=UTF-8",
        "Date",
        "Sun, 24 Mar 2019 17:36:01 GMT",
        "Expires",
        "Sun, 31 Mar 2019 17:36:01 GMT",
        "Server",
        "EOS (vny006/0452)",
        "Content-Length",
        "345",
        "Connection",
        "close"
      ]
    }

以下是nock拦截器:

export const nockServices = {
    login: {
        nockConfig: {
            path: '/auth/login',
            responseData: {
                ...loginResponse,
            },
        },
    },
};

const nockBaseUrls = [/example\.com/];

const createNockServiceIntercept = ({ method = 'post', path, responseCode = 200, responseData }) => {
    nockBaseUrls.forEach(nockBaseUrl => {
        nock(nockBaseUrl)
            .persist()
            [method](path)
            .reply(responseCode, responseData);
    });
};

export const startAllNockServiceIntercepts = () => {
    nock.cleanAll();
    Object.keys(nockServices).forEach(nockServiceName => createNockServiceIntercept({
        ...nockServices[nockServiceName].nockConfig,
    }));
};

startAllNockServiceIntercepts();

0 个答案:

没有答案