我的 Express 代碼在從 Postman 呼叫時運行良好,但在從 Mocha 呼叫時運行良好。這讓我相信我的 Mocha 測驗沒有正確設定 POST 請求的標頭中的資料(我對無引數 GET 或 POST 沒有問題,對帶引數的 GET 也沒有問題;只有帶引數的 POST)。
這是簡單的代碼,雖然我不會專注于此,因為它適用于 Postman:
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// Our unit tests send POST data in JSON format
app.use(express.json());
// Helper function - common code
function ResplyWithSentence(object, adjective)
{
console.log('GET requestuest received: object = ' object '; adjective = ' adjective);
let sentence = softwareUnderTest.MakeAsentence(object, adjective);
sentence = JSON.stringify(sentence);
response.status(200);
response.set('Content-Type', 'text/json');
response.send( sentence );
}
// Handling POST request with parameters
app.post("/makeSentenceParamsPost", (request, response) => {
const object = request.body.object;
const adjective = request.body.adjective;
ResplyWithSentence(object, adjective);
})
這是郵遞員發送的內容:
POST /makeSentenceParamsPost HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 88dfc7cc-427e-3248-ba93-286083d4c18d
{
"object": "cat",
"adjective": "sleepy"
}
這是摩卡:
it('should handle a POST request with parameters', async function(){
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://localhost:3000/makeSentenceParamsPost", false); // false === synchronous/blocking
xhttp.setRequestHeader("Content-type", "application/json");
const object = 'hubris';
const adjective = 'pervasive';
let postParameters = {'object': object,
'adjective': adjective};
xhttp.onreadystatechange = function(done) {
while(this.readyState != 4) ; // wait until "request finished and response is ready"
assert.isObject(this);
assert(this.status == 200);
assert(JSON.parse(this.responseText)['sentence'] == `The ${object} is ${adjective}`);
done();
};
postParameters = JSON.stringify(postParameters);
xhttp.send(postParameters);
});
收到的回應是The undefined is undefined.
誰能告訴我我做錯了什么?甚至如何除錯這個?
uj5u.com熱心網友回復:
使用現代 JavaScript!
我建議調查獲取。它是 ES5 的等價物并使用 Promises。它更具可讀性并且易于定制。
const fetch = require("node-fetch");
const first = 'hubris'; // can't use words like object, these are reserved.
const second = 'pervasive';
let postParameters = {'first': first,
'second': second};
const url = "http://example.com";
fetch(url, {
method : "POST",
body: postParameters,
// -- or --
// body : JSON.stringify({
// user : document.getElementById('user').value,
// ...
// })
}).then(
response => response.text() // .json(), etc.
assert.isObject(this);
assert(this.status == 200);
assert(JSON.parse(this.responseText)['sentence'] == `The ${object} is ${adjective}`);
done();
);
uj5u.com熱心網友回復:
Type 上的大寫 T 解決了這個問題:
xhttp.setRequestHeader("Content-Type", "application/json");
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429185.html
標籤:javascript 表示 mocha.js
