简体   繁体   English

如何为本地主机设置cookie以在NodeJS中测试API?

[英]How to set cookie for localhost to test API in NodeJS?

I have a web application and I'm writing APIs on NodeJS. 我有一个Web应用程序,并且正在NodeJS上编写API。

I want to test this APIs use mocha, chai and Node-rest-client and don't open browser, but I have a problem: " I don't know How to set cookie (Authorization of header) when I ran server on localhost ) 我想使用mocha,chai和Node-rest-client测试此API,并且不打开浏览器,但是我遇到一个问题:“ 当我在本地主机上运行服务器时,我不知道如何设置cookie(标头的授权)

  • First, run server on localhost:3000. 首先,在localhost:3000上运行服务器。
  • Next, run command api testing (Set cookie to server and run API testcases). 接下来,运行命令api测试(将cookie设置为服务器并运行API测试用例)。

If you're using chrome it usuall wont save cookies for loacal host try running the following to run chrome with disabled web security. 如果您使用的是chrome,通常不会为本地主机保存cookie,请尝试运行以下命令以在禁用网络安全的情况下运行chrome。

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:/Chrome dev session" --args --disable-web-security

UPDATE UPDATE

As in the documentation you can set Auth header with this : Documentation 如文档中你可以设置验证标头是: 文档

var Client = require('node-rest-client').Client;

// configure basic http auth for every request
var options_auth = { user: "admin", password: "123" };

var client = new Client(options_auth);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM