Skip to content

Instantly share code, notes, and snippets.

View JamesMessinger's full-sized avatar

James Messinger JamesMessinger

View GitHub Profile
@JamesMessinger
JamesMessinger / swagger.json
Last active September 24, 2022 14:53
ShipEngine API definition
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "ShipEngine"
},
"host": "api.shipengine.com",
"schemes": [
"https"
],
@JamesMessinger
JamesMessinger / postman-tests.js
Created July 27, 2017 19:25
Postman tests example
tests["Status code is 200"] = responseCode.code === 200;
tests["Response time is acceptable"] = responseTime < 200; // milliseconds
tests["Content-Type header is set"] = postman.getResponseHeader("Content-Type");
@JamesMessinger
JamesMessinger / set-next-request.js
Created July 27, 2017 19:24
setNextRequest() example
var customer = JSON.parse(responseBody);
if (customer.id === undefined) {
// No customer was returned, so don't run the rest of the collection
postman.setNextRequest(null);
}
else {
// Save the customer ID to a Postman environment variable
postman.setEnvironmentVariable("cust_id", customer.id);
@JamesMessinger
JamesMessinger / reuse-code.js
Created July 27, 2017 19:23
Reusing code in Postman
// First, run the common tests
eval(globals.commonTests)();
// Then run any request-specific tests
tests["Status code is 200"] = responseCode.code === 200;
@JamesMessinger
JamesMessinger / reuse-code.js
Created July 27, 2017 19:23
Saving JavaScript code to a variable in Postman
// Save common tests in a global variable
postman.setGlobalVariable("commonTests", () => {
// The Content-Type must be JSON
tests["Content-Type header is set"] = postman.getResponseHeader("Content-Type") === "application/json";
// The response time must be less than 500 milliseconds
tests["Response time is acceptable"] = responseTime < 500;
// The response body must include an "id" property
var data = JSON.parse(responseBody);
@JamesMessinger
JamesMessinger / postman-bdd.js
Created July 27, 2017 19:22
Postman BDD example
describe('Get customer info', () => {
it('should return a valid response', () => {
response.should.have.status(200);
response.should.be.json;
response.body.should.not.be.empty;
});
it('should set the Location header', () => {
response.should.have.header('Location');
@JamesMessinger
JamesMessinger / json-schema.js
Created July 27, 2017 19:21
Using JSON Schema in Postman (via an environment variable)
// Load the JSON Schema
const customerSchema = JSON.parse(environment.customerSchema);
// Test whether the response matches the schema
var customer = JSON.parse(responseBody);
tests["Customer is valid"] = tv4.validate(customer, customerSchema);
@JamesMessinger
JamesMessinger / json-schema.js
Created July 27, 2017 19:19
Using JSON Schema in Postman
// Define the JSON Schema
const customerSchema = {
"required": ["id"],
"properties": {
"id": {
"type": "integer",
"minimum": 100,
"maximum": 1000
},
"name": {
@JamesMessinger
JamesMessinger / json-pointer-example-2.yaml
Created April 15, 2016 15:27
This example demonstrates a URL-encoded JSON Pointer in a Swagger 2.0 API definition
swagger: "2.0"
info:
version: 1.0.0
title: Blog
paths:
"/blogs/{id}/posts":
parameters:
- name: "id"
in: "path"
type: "integer"
@JamesMessinger
JamesMessinger / json-pointer-example-1.yaml
Created April 15, 2016 15:25
This example demonstrates a simple URL-encoded JSON Pointer in a Swagger 2.0 API definition
swagger: "2.0"
info:
version: 1.0.0
title: Example
paths:
"/foo":
get:
responses:
200: