Created
June 28, 2018 03:20
-
-
Save simoncowie/f7b0eacf816914807c41c53d476d591d to your computer and use it in GitHub Desktop.
Mock-server callback example in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockserver = require('mockserver-node'); | |
var mockServerClient = require('mockserver-client').mockServerClient; | |
var mockserver_port = 1234; | |
mockserver | |
.start_mockserver({ serverPort: mockserver_port, verbose: true }) | |
.then( | |
function () { | |
mockServerClient("localhost", mockserver_port).mockWithCallback( | |
{ | |
path: "/user/[a-f0-9]{8}(-[a-f0-9]{4}){3}-[a-f0-9]{12}", | |
method: "GET" | |
}, | |
function (request) { | |
if (!request.headers["Authorization"]) { | |
return { | |
"statusCode": 401, | |
"reasonPhrase": "Missing Authorization header" | |
} | |
} | |
return { | |
"statusCode": 200, | |
"headers": { | |
"content-type": ["application/json"] | |
}, | |
"body": JSON.stringify({ | |
id: request.path.replace("/user/", ""), | |
username: "[email protected]" | |
name: "test tester", | |
lastLoggedInAt: "2016-05-14T02:15:15Z" | |
}) | |
}; | |
}, | |
{ | |
unlimited: true | |
} | |
).then( | |
function () { | |
console.log("created GET user expectation"); | |
}, | |
function (error) { | |
console.log(error.body); | |
} | |
); | |
}, | |
function (error) { | |
console.log(JSON.stringify(error)); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment