Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* NRExampleBaseScript.js
* Copyright (c) 2019 New Relic Inc. All Rights Reserved.
*
* This script downloads the api tester script from github
* Loads a schema definition from a url (GitHub, or locally)
* And then accesses each of the items in the
* $paths variable to verify the following $schema with.
* It uses the TV4 validator - https://github.com/geraintluff/tv4 (Public Domain)
*
* Example structure of testing.json file, the $schema object should follow the
* json-schema v4 spec http://json-schema.org/latest/json-schema-validation.html
[{
"$paths": [
"path_to_prepend_base_test_url_or_full_url_to_test.json",
"http://second_url.json"
],
"$description": "Optional description of endpoints",
"$schema": {}
}]
*
*/
var assert = require('assert');
//Add in global $http if not running in synthetics
if (typeof $http == 'undefined') {
$http = require('request');
}
// Used to synchronously load scripts
var req = require('urllib-sync').request;
// Optionally used to run locally from node (Useful for testing schema)
var fs = null; //var fs = require('fs');
// The api tester script used when called in Synthetics
var res = req('https://gist.githubusercontent.com/jvanmetre/4fdfa1ecccc72327efe77177f6ecfb43/raw/NRApiTester.js');
var encoding = res.headers['content-type'].split(' ')[1].split('=')[1];
// Load api tester script
var exports = new Function("var module = {}; module.exports = {}; " + res.data.toString(encoding) + " return module.exports;")();
var apiTester = exports.createApiTester($http, req, assert, fs); //loads the apiScript variable
//Can also be used to test locally w/ require
// var apiTester = require("./NRApiTester").createApiTester($http, req, assert, fs);
var config = {};
config.baseTestUrl = "https://api.newrelic.com";
/*
* There are three ways to load a schema
* 1. Use the schemaUrl and point to a gist / github repo's raw data
* 2. Use the schemaUrl to point to a local file (useful for running locally)
* 3. Embed the schema directly in this file
*/
config.schemaUrl = "https://gist.githubusercontent.com/jvanmetre/a9b1ee8252f4b6df15b9316a52ce15e8/raw/NRApiTestSchema.json";
// config.schemaUrl = "NRApiTestSchema.json";
// schemaAuth is used for private github repo's that host the schemaUrl
// config.schemaAuth = "";//$secure.GHE_READONLY_TOKEN;
//Optionally embed the schema right in this script
// config.testSchema = [
// {
// "$paths": ["/v2/applications.json"],
// "$description": "Tests the public api applications endpoint",
// "$schema": {
// "type": "object", "minProperties": 1, "properties": {
// "applications": {
// "type": "array", "minItems": 1, "items": {
// "type": "object", "properties": {
// "id": {"type": "number"},
// "name": {"type": "string"},
// "language": {"type": "string"}
// },
// "required": ["id", "name", "language"]
// }
// }
// }
// }
// }
// ];
//Optionally call out to an authorization service to create access token
// var accessToken = null;
// var authData = {};
// config.generateAuthentication = function (callback) {
// // Hit login service for access_token
// $http.post(LOGIN_URL + '/api/tokens', authData,
// // Callback
// function (err, response, body) {
// if (err) throw new Error('Authentication error: ' + err.message);
//
// assert.ok(response.statusCode === 200, 'Auth failed');
// accessToken = body.access_token;
// callback()
// }
// )
// };
/*
* Used to sign the actual API request
*/
config.signRequest = function (options) {
var headers = options.headers;
if (null == headers) {
headers = {}
}
headers['X-Api-Key'] = $secure.REST_API_KEY;
};
// config.signRequest = signRequest;
/*
* Used to kick off the api tester, which will iterate through
* $paths and check the returns against the $schema from the
* apiTester.schemaUrl
*/
apiTester.check(config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.