How to use verifyResponseHeaders method in apimocker

Best JavaScript code snippet using apimocker

test-functional.js

Source:test-functional.js Github

copy

Full Screen

...43 req.write(postData);44 }45 req.end();46 }47 function verifyResponseHeaders(httpReqOptions, expected, done) {48 var req = http.request(httpReqOptions, function(res) {49 res.setEncoding('utf8');50 res.on('data', function () { //chunk) {51 // console.log(res.headers);52 var expectedKeys = _.keys(expected);53 _.each(expectedKeys, function(key) {54 expect(res.headers[key]).to.equal(expected[key]);55 });56 done();57 });58 });59 req.end();60 }61 function verifyResponseStatus(httpReqOptions, postData, expectedStatus, done) {62 var req = http.request(httpReqOptions, function(res) {63 res.setEncoding('utf8');64 res.on('data', function() {65 // console.log("data event, status: " + res.statusCode);66 expect(res.statusCode).to.equal(expectedStatus);67 });68 res.on('end', function() {69 // console.log("status: " + res.statusCode);70 expect(res.statusCode).to.equal(expectedStatus);71 done();72 });73 });74 if (postData) {75 req.write(postData);76 }77 req.end();78 }79 describe("old file format:", function() {80 before(function startMockerForFuncTests() {81 mocker = apiMocker.createServer({quiet: false}).setConfigFile("test/test-old-config.json");82 mocker.start();83 });84 it('returns correct data for basic get request', function(done) {85 var reqOptions = httpReqOptions("/first");86 verifyResponseBody(reqOptions, null, {"king": "greg"}, done);87 });88 it('returns correct content-type for json response', function(done) {89 var reqOptions = httpReqOptions("/first");90 verifyResponseHeaders(reqOptions, {"content-type": "application/json"}, done);91 });92 it('returns correct content-type for xml response', function(done) {93 var reqOptions = httpReqOptions("/queen");94 verifyResponseHeaders(reqOptions, {"content-type": "application/xml"}, done);95 });96 it('returns correct data for basic post request', function(done) {97 var reqOptions = httpReqOptions("/king");98 reqOptions.method = "POST";99 verifyResponseBody(reqOptions, null, {"king": "greg"}, done);100 });101 it('returns 404 for incorrect path', function(done) {102 var reqOptions = httpReqOptions("/first");103 reqOptions.method = "POST";104 verifyResponseStatus(reqOptions, "{}", 404, done);105 });106 });107 describe('new config file format:', function() {108 before(function(done) {109 mocker.setConfigFile("test/test-config.json");110 var req, reqOptions = httpReqOptions();111 reqOptions.path = "/admin/reload";112 req = http.request(reqOptions, function(res) {113 res.setEncoding('utf8');114 res.on('data', function () {115 expect(res.statusCode).to.equal(200);116 done();117 });118 });119 req.end();120 });121 it('sets a basic route', function(done) {122 var reqOptions = httpReqOptions("/first");123 reqOptions.method = "get";124 verifyResponseBody(reqOptions, null, {"king": "greg"}, done);125 });126 it('returns a custom content type', function(done) {127 var reqOptions = httpReqOptions("/first");128 verifyResponseHeaders(reqOptions, {"content-type": "foobar"}, done);129 });130 it('returns correct content-type for json response, with nested path', function(done) {131 var reqOptions = httpReqOptions("/nested/ace");132 verifyResponseHeaders(reqOptions, {"content-type": "application/json"}, done);133 });134 it('returns 404 for incorrect path, after reload', function(done) {135 var reqOptions = httpReqOptions();136 reqOptions.method = "post";137 reqOptions.path = "/king";138 verifyResponseStatus(reqOptions, "{}", 404, done);139 });140 it('allows domains specified in config file', function(done) {141 var reqOptions = httpReqOptions("/first");142 verifyResponseHeaders(reqOptions, {'access-control-allow-origin': "abc"}, done);143 });144 it('allows headers as specified in config file', function(done) {145 var reqOptions = httpReqOptions("/first");146 verifyResponseHeaders(reqOptions, {'access-control-allow-headers': 'Content-Type,my-custom-header'}, done);147 });148 it('returns correct file for switch param in json request', function(done) {149 var postData = '{"customerId": 1234}',150 postOptions = httpPostOptions("/nested/ace", postData),151 expected = {152 "ace": "greg",153 "note": "request contained customerId = 1234"154 };155 verifyResponseBody(postOptions, postData, expected, done);156 });157 it('returns base file when no match for switch param in json request', function(done) {158 var postData = '{"customerId": 124}',159 postOptions = httpPostOptions("/nested/ace", postData),160 expected = {161 "ace": "greg"162 };163 verifyResponseBody(postOptions, postData, expected, done);164 });165 it('returns base file when no switch param passed in json request', function(done) {166 var postData = '{"phonenumber": 124}',167 postOptions = httpPostOptions("/nested/ace", postData),168 expected = {169 "ace": "greg"170 };171 verifyResponseBody(postOptions, postData, expected, done);172 });173 it('returns correct file for switch param in query string', function(done) {174 var reqOptions = httpReqOptions("/nested/ace?customerId=1234"),175 expected = {176 "ace": "greg",177 "note": "request contained customerId = 1234"178 };179 verifyResponseBody(reqOptions, null, expected, done);180 });181 it('returns correct httpStatus when switches match', function(done) {182 var postData = '{"userId": "user1", "password": "good"}',183 postOptions = httpPostOptions("/login", postData);184 verifyResponseStatus(postOptions, postData, 200, done);185 });186 it('returns correct httpStatus when switch does not match', function(done) {187 var postOptions = httpPostOptions("/login", "{}");188 verifyResponseStatus(postOptions, "{}", 401, done);189 });190 it("returns httpStatus of 200 if not set", function(done) {191 verifyResponseStatus(httpReqOptions("/first"), null, 200, done);192 });193 it("returns httpStatus specified in config file, when contentType is passed in", function(done) {194 var reqOptions = httpPostOptions("/protected", '{}');195 reqOptions.method = "put";196 verifyResponseStatus(reqOptions, "{}", 403, done);197 });198 it("returns httpStatus 204 specified in config file", function(done) {199 var reqOptions = httpReqOptions("/second");200 reqOptions.method = "delete";201 verifyResponseStatus(reqOptions, null, 204, done);202 });203 // TODO: Fix this test... it fails intermittently, due to timing problems.204 xit("returns correct mock file after admin/setMock was called twice", function(done) {205 var postData = '{"verb":"get", "serviceUrl":"third", "mockFile":"king.json"}',206 postOptions = httpPostOptions("/admin/setMock", postData),207 expected = {208 "verb":"get",209 "serviceUrl":"third",210 "mockFile":"king.json",211 "httpStatus": 200212 };213 verifyResponseBody(postOptions, postData, expected);214 verifyResponseBody(httpReqOptions("/third"), null, {king: "greg"});215 // change route, and verify again216 postData = '{"verb":"get", "serviceUrl":"third", "mockFile":"ace.json"}';217 postOptions = httpPostOptions("/admin/setMock", postData);218 expected = {219 "verb":"get",220 "serviceUrl":"third",221 "mockFile":"ace.json",222 "httpStatus": 200223 };224 verifyResponseBody(postOptions, postData, expected);225 verifyResponseBody(httpReqOptions("/third"), null, {ace: "greg"}, done);226 });227 it("returns the headers as specified in the config file", function(done) {228 var reqOptions = httpReqOptions("/firstheaders");229 verifyResponseHeaders(reqOptions, {"x-requested-by": "4c2df03a17a803c063f21aa86a36f6f55bdde1f85b89e49ee1b383f281d18c09c2ba30654090df3531cd2318e3c", "dummyheader": "dummyvalue", "content-type": "foobar"}, done);230 });231 });...

Full Screen

Full Screen

FetchSpec.js

Source:FetchSpec.js Github

copy

Full Screen

...25 it("handles method type in any case", function () {26 return verifyResponseBody("post", data, "foo=b ar&baz=yolo");27 });28 it("sets provided headers", function () {29 return verifyResponseHeaders("POST", headers, headers);30 });31 it("sets Content-type header for POST if not provided", function () {32 return verifyResponseHeaders("POST", {}, {33 "Content-type": "application/x-www-form-urlencoded;charset=utf-8"34 });35 });36 it("catch 404", function (done) {37 handleJSON("/bad", "POST", "").catch(function () {38 done();39 });40 });41 });42 describe("GET", function () {43 it("GETs data (URLencoded string)", function () {44 return verifyResponseBody("GET", data, "foo=b ar&baz=yolo");45 });46 it("handles method type in any case", function () {47 return verifyResponseBody("get", data, "foo=b ar&baz=yolo");48 });49 it("sets provided headers", function () {50 return verifyResponseHeaders("GET", headers, headers);51 });52 it("catch 404", function (done) {53 handleJSON("/bad", "GET", "").catch(function () {54 done();55 });56 });57 });58 function handleJSON(url, method, data, headers) {59 var then = $.fetch(url, {60 method: method,61 data: data,62 headers: headers || {}63 });64 server.respond();65 return then;66 }67 function verifyResponseBody(method, data, expected) {68 var promise = handleJSON("/json", method, data);69 return promise.then(function (response) {70 expect(response.response).to.equal(encodeURI(expected));71 });72 }73 function verifyResponseHeaders(method, headers, expected) {74 var promise = handleJSON("/json", method, "", headers);75 return promise.then(function (response) {76 expect(response.responseHeaders).to.deep.equal(expected);77 });78 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var apimocker = require('apimocker');2var http = require('http');3var options = {4};5var req = http.request(options, function (res) {6 res.setEncoding('utf8');7 res.on('data', function (chunk) {8 console.log(chunk);9 });10});11req.write(JSON.stringify({12 "request": {13 },14 "response": {15 "headers": {16 }17 }18}));19req.end();20var apimocker = require('apimocker');21var http = require('http');22var options = {23};24var req = http.request(options, function (res) {25 res.setEncoding('utf8');26 res.on('data', function (chunk) {27 console.log(chunk);28 });29});30req.write(JSON.stringify({31 "request": {32 "headers": {33 }34 },35 "response": {36 }37}));38req.end();39var apimocker = require('apimocker');40var http = require('http');41var options = {42};43var req = http.request(options, function (res) {44 res.setEncoding('utf8');45 res.on('data', function (chunk) {46 console.log(chunk);47 });48});49req.write(JSON.stringify({50 "request": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var apimocker = require('apimocker');2var options = {3};4apimocker.createServer(options).start(function() {5 var mock = apimocker.loadMock('test');6 console.log(mock.verifyResponseHeaders('GET', '/test', {7 }));8});9var apimocker = require('apimocker');10var options = {11};12apimocker.createServer(options).start(function() {13 var mock = apimocker.loadMock('test');14 console.log(mock.verifyResponseBody('GET', '/test', {15 }));16});17var apimocker = require('apimocker');18var options = {19};20apimocker.createServer(options).start(function() {21 var mock = apimocker.loadMock('test');22 console.log(mock.verifyResponseStatus('GET', '/test', 200));23});

Full Screen

Using AI Code Generation

copy

Full Screen

1var apimocker = require('apimocker');2 if(err) {3 console.log('Error: ' + err);4 }5 else {6 console.log('Result: ' + result);7 }8});9var apimocker = require('apimocker');10 if(err) {11 console.log('Error: ' + err);12 }13 else {14 console.log('Result: ' + result);15 }16});17var apimocker = require('apimocker');18 if(err) {19 console.log('Error: ' + err);20 }21 else {22 console.log('Result: ' + result);23 }24});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run apimocker automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful