Skip to content

Commit

Permalink
Updated error codes in testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
sunil-lakshman committed Dec 17, 2024
1 parent 8b9f81d commit 894080a
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions test/entry/findone.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,55 +743,35 @@ test('findOne: .except() - For the reference - Array', function(assert) {
* HTTP Error Handling
* !*/

test('findOne: should handle 404 Not Found error', function(assert) {
const Query = Stack.ContentType(contentTypes.invalid_type).Query();
test('findOne: should handle 422 Unprocessable Entity error', function(assert) {
const Query = Stack.ContentType("invalid_content_type").Query();

Query
.toJSON()
.findOne()
.then(function success() {
assert.fail("Expected 404 error but got a successful response.");
assert.fail("Expected 422 error but got a successful response.");
assert.end();
}, function error(err) {
assert.equal(err.http_code, 404, 'Should return HTTP status 404.');
assert.ok(err.http_message, 'Error message should be present.');
console.error("Error:", err.http_message);
assert.equal(err.http_code, 422, 'Should return HTTP status 422.');
assert.ok(err.http_message, 'Unprocessable Entity');
assert.end();
});
});

test('findOne: should handle 401 Unauthorized error', function(assert) {
test('findOne: should handle 412 Unauthorized error', function(assert) {
Stack.headers = { authorization: 'InvalidAPIKey' }; // Simulating an invalid API key
const Query = Stack.ContentType(contentTypes.source).Query();

Query
.toJSON()
.findOne()
.then(function success() {
assert.fail("Expected 401 error but got a successful response.");
assert.fail("Expected 412 error but got a successful response.");
assert.end();
}, function error(err) {
assert.equal(err.http_code, 401, 'Should return HTTP status 401.');
assert.ok(err.http_message, 'Error message should be present.');
console.error("Error:", err.http_message);
assert.equal(err.http_code, 412, 'Should return HTTP status 412.');
assert.ok(err.http_message, 'Precondition Failed.');
assert.end();
});
});

test('findOne: should handle 500 Internal Server Error', function(assert) {
const mockStack = Contentstack.Stack({ ...init.stack, host: 'invalid.host' }); // Simulating a server error
const Query = mockStack.ContentType(contentTypes.source).Query();

Query
.toJSON()
.findOne()
.then(function success() {
assert.fail("Expected 500 error but got a successful response.");
assert.end();
}, function error(err) {
assert.equal(err.http_code, 500, 'Should return HTTP status 500.');
assert.ok(err.http_message, 'Error message should be present.');
console.error("Error:", err.http_message);
assert.end();
});
});

0 comments on commit 894080a

Please sign in to comment.