Skip to content

Instantly share code, notes, and snippets.

@t404
Last active November 25, 2020 08:20
Show Gist options
  • Save t404/164d23003f17c71f51fcb7550a69f8b3 to your computer and use it in GitHub Desktop.
Save t404/164d23003f17c71f51fcb7550a69f8b3 to your computer and use it in GitHub Desktop.
[Postman Test Script] #postman
/**
* Postman Test Script
* URL: https://doing-api.herokuapp.com/api/0.1/events/?latitude=27.782887&longitude=-82.6329571
* Description: Events in St. Petersbug, FL
* Method: GET
*/
var jsonData = JSON.parse(responseBody);
// Test Request
tests["REQUEST: Response time is less than 200ms"] = responseTime < 200;
tests["REQUEST: Status code is 200"] = responseCode.code === 200;
// Check for API Errors
tests["META: Error Free"] = jsonData.error === false;
// Check META Data
tests["META: has Meta Total"] = jsonData.meta.total > 0;
tests["META: has Meta Showing"] = jsonData.meta.showing === 30;
tests["META: has Meta Pages"] = jsonData.meta.pages >= 1;
tests["META: has Meta Page"] = jsonData.meta.page === 1;
// Check Events
tests["META: has 30 Events"] = jsonData.data.length === 30;
for (var i = 0; i < jsonData.data.length; i++) {
tests["EVENT (required): " + ( i + 1) + " has ID"] = (typeof jsonData.data[i].id === 'string' && jsonData.data[i].id.length > 0);
tests["EVENT (required): " + ( i + 1) + " has Name"] = (typeof jsonData.data[i].name === 'string' && jsonData.data[i].name.length > 0);
tests["EVENT (required): " + ( i + 1) + " has Start Datetime"] = (typeof jsonData.data[i].start_datetime === 'string' && jsonData.data[i].start_datetime.length > 0);
tests["EVENT (required): " + ( i + 1) + " has Start Time"] = (typeof jsonData.data[i].start_time === 'string' && jsonData.data[i].start_time.length > 0);
tests["EVENT (required): " + ( i + 1) + " has End Datetime"] = (typeof jsonData.data[i].end_datetime === 'string' && jsonData.data[i].end_datetime.length > 0);
tests["EVENT (required): " + ( i + 1) + " has End Time"] = (typeof jsonData.data[i].end_time === 'string' && jsonData.data[i].end_time.length > 0);
tests["EVENT (required): " + ( i + 1) + " has URL"] = (typeof jsonData.data[i].url === 'string' && jsonData.data[i].url.length > 0);
tests["EVENT (required): " + ( i + 1) + " has Cost"] = (typeof jsonData.data[i].cost === 'number' && jsonData.data[i].cost >= 0);
tests["EVENT (expected): " + ( i + 1) + " has Description"] = (typeof jsonData.data[i].description === 'string' && jsonData.data[i].description.length > 0);
tests["EVENT (expected): " + ( i + 1) + " has Category"] = (typeof jsonData.data[i].categories === 'object' && jsonData.data[i].categories.length > 0);
tests["EVENT (expected): " + ( i + 1) + " has Distance"] = (typeof jsonData.data[i].distance === 'object' && jsonData.data[i].distance.length > 0);
tests["LOCATION (required): " + ( i + 1) + " has ID"] = (typeof jsonData.data[i].location === 'object' && typeof jsonData.data[i].location.id === 'string' && jsonData.data[i].location.id.length > 0);
tests["LOCATION (required): " + ( i + 1) + " has Name"] = (typeof jsonData.data[i].location === 'object' && typeof jsonData.data[i].location.name === 'string' && jsonData.data[i].location.name.length > 0);
tests["LOCATION (required): " + ( i + 1) + " has Address"] = (typeof jsonData.data[i].location === 'object' && typeof jsonData.data[i].location.address === 'string' && jsonData.data[i].location.address.length > 0);
tests["LOCATION (required): " + ( i + 1) + " has City"] = (typeof jsonData.data[i].location === 'object' && typeof jsonData.data[i].location.city === 'string' && jsonData.data[i].location.city.length > 0);
tests["LOCATION (required): " + ( i + 1) + " has State"] = (typeof jsonData.data[i].location === 'object' && typeof jsonData.data[i].location.state === 'string' && jsonData.data[i].location.state.length > 0);
tests["LOCATION (required): " + ( i + 1) + " has Latitude"] = (typeof jsonData.data[i].location === 'object' && typeof jsonData.data[i].location.latitude === 'number' && jsonData.data[i].location.latitude.length > 0);
tests["LOCATION (required): " + ( i + 1) + " has Longitude"] = (typeof jsonData.data[i].location === 'object' && typeof jsonData.data[i].location.longitude === 'number' && jsonData.data[i].location.longitude.length > 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment