I am running a test and if it fails the result is
Status code is 200 | AssertionError: expected response to have status
code 200 but got 404
It would be useful to see the result failed message. I am printing it to the console, but not sure how to get it show up on the test results as well.
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
const response = pm.response.json();
console.log(response);
You could add it to the test name, like this:
const response = pm.response.json();
pm.test(`Test something - ${response}`, () => {...})
Depending on the data returned, this would look awful for larger responses.
Not sure what this is giving you though...the console is the best place to print that type of debugging information.
Related
Given the following transaction reply how would I form a pm.test assertion for it?
{
"data": {
"clearEmployerClaim": true
}
}
This isn't working for me:
pm.test("clear employer claim status returned", () => {
const response = pm.response.json();
pm.expect(response.clearEmployerClaim).to.be.true;
});
I would normally do it like this but I'm getting type errors when running this via Jenkins/Newman. It works fine run via Postman.
tests["C536773447 clear employer claim status returned"] = body.data.clearEmployerClaim === true;
Any help is appreciated.
You missed one level here.
pm.expect(response.clearEmployerClaim).to.be.true; --> pm.expect(response.data.clearEmployerClaim).to.be.true;
From below Response, I want to fetch the value of "responseCode" and store temporarily. If a value is 1 then on Console, I want to write "Test PASS". Can anyone share code for this test?
{
"data":{
"transactionId":"$1"
},
"responseMessage":"Transaction successfully done. Transaction Id : txn_15594028419901124218",
"responseCode":1
}
I tried to use the following code to set the variable:
var jsonData = JSON.parse(responseBody);
pm.globals.set("responseCode",jsonData.data.responseCode);
This basic test would check that value in the response, store the variable and also write Test PASS to the console
pm.test("Check the Response Code is 1", () => {
pm.expect(pm.response.json().responseCode).to.eql(1);
pm.globals.set("responseCode", pm.response.json().responseCode)
console.log("Test PASS")
});
This doesn't account for the test failing and writing Test FAIL to the console, you kind of get that anyway in the Postman UI.
If you didn't want to wrap this in a test, you could just do something like:
if(pm.response.json().responseCode === 1){
pm.globals.set("responseCode", pm.response.json().responseCode)
console.log("Test PASS")
}
else {
console.log("Test FAIL")
}
I have a POST request in Postman, with the following test
pm.test("CREATED - Status code is 201", function () {
pm.response.to.have.status(201);
});
var namespaceLink = postman.getResponseHeader("Location");
var namespaceId = namespaceLink.substring(namespaceLink.lastIndexOf('/') + 1);
pm.environment.set("IdOfNamespace", namespaceId);
The request is failing with "400 Bad Request".
In this case, I would expect that Test fails with something like "Expected 201 but got 400", but the test is failing with following "There was an error in evaluating the test script: TypeError: Cannot read property 'substring' of undefined"
I also receive the following message in the response body: "Namespace 'AUTO_NS' is already using this code"
Since the request is run as part of the automated test (with a lot of requests) and then generate a report, I would like to set up the test the way it will be more clear in the report on the reason of the failure. I.e. message from response body.
Can you please help me on how I can do it (as part of the test)?
You just can simply have your code inside the test function. When your pm.response.to.have.status(201); assertion fails, chai throws AssertionError and that means that the other lines inside your test function won't be executed. In other cases, you're good to go. Try this:
pm.test("CREATED - Status code is 201", () => {
pm.response.to.have.status(201);
const namespaceLink = postman.getResponseHeader("Location");
const namespaceId = namespaceLink.substring(namespaceLink.lastIndexOf('/') + 1);
pm.environment.set("IdOfNamespace", namespaceId);
});
I have an API Gateway with a LAMBDA_PROXY Integration Request Type. Upon calling context.succeed in the Lambda, the response header is sent back with code 302 as expected (shown below). However, I want to handle 500 and 404 errors, and the only thing I am sure about so far, is that I am returning the error incorrectly as I am getting 502 Bad Gateway. What is wrong with my context.fail?
Here is my handler.js
const handler = (event, context) => {
//event consists of hard coded values right now
getUrl(event.queryStringParameters)
.then((result) => {
const parsed = JSON.parse(result);
let url;
//handle error message returned in response
if (parsed.error) {
let error = {
statusCode: 404,
body: new Error(parsed.error)
}
return context.fail(error);
} else {
url = parsed.source || parsed.picture;
return context.succeed({
statusCode: 302,
headers: {
Location : url
}
});
}
});
};
If you throw an exception within the Lambda function (or context.fail), API Gateway reads it as if something had gone wrong with your backend and returns 502. If this is a runtime exception you expect and want to return a 500/404, use the context.succeed method with the status code you want and message:
if (parsed.error) {
let error = {
statusCode: 404,
headers: { "Content-Type": "text/plain" } // not sure here
body: new Error(parsed.error)
}
return context.succeed(error);
I had the same problem, in my case the issue was that my function was not returning anything in context.done(). So instead of context.done(null), I did context.done(null, {});
I've gotten 502's from multiple things. Here are the ones I have figured out so far.
Answer 1:
claudia generate-serverless-express-proxy --express-module {src/server?}
If you are not using claudia and express, this answer won't help you.
Answer 2:
Lambda function->Basic Settings->Timeout. Increase it to something reasonable. It defaults to 3 seconds. But the first time building it typically takes longer.
I had a problem like this, I was returning JSON as a JavaScript Object in the body, but you are supposed to return it as a string. All I had to do was do a JSON.stringify(dataobject) to convert the JSON into a string before returning it.
https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/
I am trying to use ember-data to get a simple registration form to save on my server. The call technically works, but the success callback is never trigger on the promise, and I have no idea why.
The server receives the data from the front end and successfully saves it to the database. It then returns status code 201 for CREATED. I can see the successful response happening in the Chrome debugger. But even when the server responds with a successful status, the error callback is triggered on the save's promise. I've confirmed this happens every time by putting a debugger; statement in the error callback.
My router's model is hooked up like this:
model: function() {
return this.store.createRecord('registerUser');
}
And I have a simple register function in my controller:
register: function() {
var self = this;
this.get('model').save().then(function() {
self.transitionToRoute('index');
}, function(resp) {
if (resp.responseJSON) {
self.get('model').set('errors', resp.responseJSON.errors);
}
});
}
Every time my server comes back with a response, success or failure, the failure callback is hit. If I have errors in the response (for invalid data or something), the errors are successfully displayed in the form. I can see the request coming in properly, and the data is stored in the database. So, the save is technically successful, but ember doesn't seem to know that it is even though a successful 201 status is returned from the server (which can be verified in the Chrome debugger).
The only thing I can think of is that ember-data's adapter is doing something that I'm not aware of, but I am just using the default RESTAdapter and haven't touched it. Is there anything else
If it makes a difference, the server is running Play 1.2.5. I don't know if that makes a difference in the response's header or something like that.
Any help would be greatly appreciated. Thank you for your time!
Mike
SOLUTION
So, the issue was to do with the JSON response. The two problems:
I did not include an ID in the response
I did not "wrap" the response in a "registerUser". This is necessary to match the model name.
Below is a valid response:
{
"registerUser": {
"id": 11,
"email": "mike999#test.com",
"password": "12345",
"password2": "12345",
"name": "Mike"
}
}
Ember Data is expecting the model in the response, so sending back a success http status doesn't mean it will hit the success endpoint. When it tries to serialize your response (or lack of response) it's probably failing which would be why it's hitting the failure function. A big reason for the response is the id of the record.
The model returned should be in the following format
{
registerUser:{
id: "123",
attr: "asdf"
}
}
https://github.com/emberjs/data/blob/master/TRANSITION.md