forked from loafle/openapi-generator-original
Restore JS tests (#13547)
* restore js tests * test only js in circleci * trigger build failure * Revert "trigger build failure" This reverts commit 7e8c34e8234a87660590f95e2f761b026edb1d4f. * Revert "test only js in circleci" This reverts commit e261429339a062077fadfeabfda02aaef30308af.
This commit is contained in:
parent
00604aff59
commit
210a394e0d
423
samples/client/petstore/javascript-es6/test/ApiClientTest.js
Normal file
423
samples/client/petstore/javascript-es6/test/ApiClientTest.js
Normal file
@ -0,0 +1,423 @@
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
var expect = require('expect.js');
|
||||
var OpenAPIPetstore = require('../src/index');
|
||||
var sinon = require('sinon');
|
||||
}
|
||||
|
||||
var apiClient = OpenAPIPetstore.ApiClient.instance;
|
||||
|
||||
describe('ApiClient', function() {
|
||||
describe('defaults', function() {
|
||||
it('should have correct default values with the default API client', function() {
|
||||
expect(apiClient).to.be.ok();
|
||||
expect(apiClient.basePath).to.be('http://petstore.swagger.io:80/v2');
|
||||
expect(apiClient.authentications).to.eql({
|
||||
petstore_auth: {type: 'oauth2'},
|
||||
bearer_test: {type: 'bearer'},
|
||||
http_basic_test: {type: 'basic'},
|
||||
api_key: {type: 'apiKey', 'in': 'header', name: 'api_key'},
|
||||
api_key_query: {type: 'apiKey', 'in': 'query', name: 'api_key_query'},
|
||||
/* comment out the following as these fake security def (testing purpose)
|
||||
* are removed from the spec, we'll add these back after updating the
|
||||
* petstore server
|
||||
*
|
||||
test_http_basic: {type: 'basic'},
|
||||
test_api_client_id: {
|
||||
type: 'apiKey',
|
||||
'in': 'header',
|
||||
name: 'x-test_api_client_id'
|
||||
},
|
||||
test_api_client_secret: {
|
||||
type: 'apiKey',
|
||||
'in': 'header',
|
||||
name: 'x-test_api_client_secret'
|
||||
},
|
||||
test_api_key_query: {
|
||||
type: 'apiKey',
|
||||
'in': 'query',
|
||||
name: 'test_api_key_query'
|
||||
},
|
||||
test_api_key_header: {
|
||||
type: 'apiKey',
|
||||
'in': 'header',
|
||||
name: 'test_api_key_header'
|
||||
}*/
|
||||
});
|
||||
});
|
||||
|
||||
it('should have correct default values with new API client and can customize it', function() {
|
||||
var newClient = new OpenAPIPetstore.ApiClient;
|
||||
expect(newClient.basePath).to.be('http://petstore.swagger.io:80/v2');
|
||||
expect(newClient.buildUrl('/abc', {})).to.be('http://petstore.swagger.io:80/v2/abc');
|
||||
|
||||
newClient.basePath = 'http://example.com';
|
||||
expect(newClient.basePath).to.be('http://example.com');
|
||||
expect(newClient.buildUrl('/abc', {})).to.be('http://example.com/abc');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#paramToString', function() {
|
||||
it('should return empty string for null and undefined', function() {
|
||||
expect(apiClient.paramToString(null)).to.be('');
|
||||
expect(apiClient.paramToString(undefined)).to.be('');
|
||||
});
|
||||
|
||||
it('should return string', function() {
|
||||
expect(apiClient.paramToString('')).to.be('');
|
||||
expect(apiClient.paramToString('abc')).to.be('abc');
|
||||
expect(apiClient.paramToString(123)).to.be('123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#buildCollectionParam', function() {
|
||||
var param;
|
||||
|
||||
beforeEach(function() {
|
||||
param = ['aa', 'bb', 123];
|
||||
});
|
||||
|
||||
it('works for csv', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'csv')).to.be('aa,bb,123');
|
||||
});
|
||||
|
||||
it('works for ssv', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'ssv')).to.be('aa bb 123');
|
||||
});
|
||||
|
||||
it('works for tsv', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'tsv')).to.be('aa\tbb\t123');
|
||||
});
|
||||
|
||||
it('works for pipes', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'pipes')).to.be('aa|bb|123');
|
||||
});
|
||||
|
||||
it('works for multi', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'multi')).to.eql(['aa', 'bb', '123']);
|
||||
});
|
||||
|
||||
it('fails for invalid collection format', function() {
|
||||
expect(function() { apiClient.buildCollectionParam(param, 'INVALID'); }).to.throwError();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#buildUrl', function() {
|
||||
it('should work without path parameters in the path', function() {
|
||||
expect(apiClient.buildUrl('/abc', {})).to
|
||||
.be('http://petstore.swagger.io:80/v2/abc');
|
||||
expect(apiClient.buildUrl('/abc/def?ok', {id: 123})).to
|
||||
.be('http://petstore.swagger.io:80/v2/abc/def?ok');
|
||||
});
|
||||
|
||||
it('should work with path parameters in the path', function() {
|
||||
expect(apiClient.buildUrl('/{id}', {id: 123})).to
|
||||
.be('http://petstore.swagger.io:80/v2/123');
|
||||
expect(apiClient.buildUrl('/abc/{id}/{name}?ok', {id: 456, name: 'a b'})).to.
|
||||
be('http://petstore.swagger.io:80/v2/abc/456/a%20b?ok');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#isJsonMime', function() {
|
||||
it('should return true for JSON MIME', function() {
|
||||
expect(apiClient.isJsonMime('application/json')).to.be(true);
|
||||
expect(apiClient.isJsonMime('application/json; charset=UTF8')).to.be(true);
|
||||
expect(apiClient.isJsonMime('APPLICATION/JSON')).to.be(true);
|
||||
});
|
||||
|
||||
it('should return false for non-JSON MIME', function() {
|
||||
expect(apiClient.isJsonMime('')).to.be(false);
|
||||
expect(apiClient.isJsonMime('text/plain')).to.be(false);
|
||||
expect(apiClient.isJsonMime('application/xml')).to.be(false);
|
||||
expect(apiClient.isJsonMime('application/jsonp')).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('multipleServers', function() {
|
||||
describe('host settings', function() {
|
||||
var hosts = apiClient.hostSettings();
|
||||
it('should have proper 1st URL', function() {
|
||||
expect(hosts[0]['url']).to.be('http://{server}.swagger.io:{port}/v2');
|
||||
expect(hosts[0]['variables']['server']['default_value']).to.be('petstore');
|
||||
});
|
||||
|
||||
it('should have proper 2nd URL', function() {
|
||||
expect(hosts[1]['url']).to.be('https://localhost:8080/{version}');
|
||||
expect(hosts[1]['variables']['version']['default_value']).to.be('v2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('get host from settings', function() {
|
||||
it('should have correct default URL', function() {
|
||||
expect(apiClient.getBasePathFromSettings(0)).to.be('http://petstore.swagger.io:80/v2');
|
||||
});
|
||||
|
||||
it('should have correct URL with port 8080', function() {
|
||||
expect(apiClient.getBasePathFromSettings(0, {'port': '8080'})).to.be('http://petstore.swagger.io:8080/v2');
|
||||
});
|
||||
|
||||
it('should have correct URL with port 8080 and dev-petstore', function() {
|
||||
expect(apiClient.getBasePathFromSettings(0, {'server': 'dev-petstore', 'port': '8080'})).to.be('http://dev-petstore.swagger.io:8080/v2');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#applyAuthToRequest', function() {
|
||||
var req, newClient;
|
||||
|
||||
beforeEach(function() {
|
||||
req = {
|
||||
auth: function() {},
|
||||
set: function() {},
|
||||
query: function() {}
|
||||
};
|
||||
sinon.stub(req, 'auth');
|
||||
sinon.stub(req, 'set');
|
||||
sinon.stub(req, 'query');
|
||||
newClient = new OpenAPIPetstore.ApiClient();
|
||||
});
|
||||
|
||||
describe('basic', function() {
|
||||
var authName = 'testBasicAuth';
|
||||
var authNames = [authName];
|
||||
var auth;
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[authName] = {type: 'basic'};
|
||||
auth = newClient.authentications[authName];
|
||||
});
|
||||
|
||||
it('sets auth header with username and password set', function() {
|
||||
auth.username = 'user';
|
||||
auth.password = 'pass';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.auth);
|
||||
// 'dXNlcjpwYXNz' is base64-encoded string of 'user:pass'
|
||||
sinon.assert.calledWithMatch(req.auth, 'user', 'pass');
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('sets header with only username set', function() {
|
||||
auth.username = 'user';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.auth);
|
||||
// 'dXNlcjo=' is base64-encoded string of 'user:'
|
||||
sinon.assert.calledWithMatch(req.auth, 'user', '');
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('sets header with only password set', function() {
|
||||
auth.password = 'pass';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.auth);
|
||||
// 'OnBhc3M=' is base64-encoded string of ':pass'
|
||||
sinon.assert.calledWithMatch(req.auth, '', 'pass');
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('does not set header when username and password are not set', function() {
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
});
|
||||
|
||||
describe('apiKey', function() {
|
||||
var authName = 'testApiKey';
|
||||
var authNames = [authName];
|
||||
var auth;
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[authName] = {type: 'apiKey', name: 'api_key'};
|
||||
auth = newClient.authentications[authName];
|
||||
});
|
||||
|
||||
it('sets api key in header', function() {
|
||||
auth.in = 'header';
|
||||
auth.apiKey = 'my-api-key';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.set);
|
||||
sinon.assert.calledWithMatch(req.set, {'api_key': 'my-api-key'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('sets api key in query', function() {
|
||||
auth.in = 'query';
|
||||
auth.apiKey = 'my-api-key';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.query);
|
||||
sinon.assert.calledWithMatch(req.query, {'api_key': 'my-api-key'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
});
|
||||
|
||||
it('sets api key in header with prefix', function() {
|
||||
auth.in = 'header';
|
||||
auth.apiKey = 'my-api-key';
|
||||
auth.apiKeyPrefix = 'Key';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.set);
|
||||
sinon.assert.calledWithMatch(req.set, {'api_key': 'Key my-api-key'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('works when api key is not set', function() {
|
||||
auth.in = 'query';
|
||||
auth.apiKey = null;
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.notCalled(req.query);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
});
|
||||
});
|
||||
|
||||
describe('oauth2', function() {
|
||||
var authName = 'testOAuth2';
|
||||
var authNames = [authName];
|
||||
var auth;
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[authName] = {type: 'oauth2'};
|
||||
auth = newClient.authentications[authName];
|
||||
});
|
||||
|
||||
it('sets access token in header', function() {
|
||||
auth.accessToken = 'my-access-token';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.set);
|
||||
sinon.assert.calledWithMatch(req.set, {'Authorization': 'Bearer my-access-token'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('works when access token is not set', function() {
|
||||
auth.accessToken = null;
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.notCalled(req.query);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
});
|
||||
});
|
||||
|
||||
describe('apiKey and oauth2', function() {
|
||||
var apiKeyAuthName = 'testApiKey';
|
||||
var oauth2Name = 'testOAuth2';
|
||||
var authNames = [apiKeyAuthName, oauth2Name];
|
||||
var apiKeyAuth, oauth2;
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[apiKeyAuthName] = {type: 'apiKey', name: 'api_key', 'in': 'query'};
|
||||
newClient.authentications[oauth2Name] = {type: 'oauth2'};
|
||||
apiKeyAuth = newClient.authentications[apiKeyAuthName];
|
||||
oauth2 = newClient.authentications[oauth2Name];
|
||||
});
|
||||
|
||||
it('works when setting both api key and access token', function() {
|
||||
apiKeyAuth.apiKey = 'my-api-key';
|
||||
oauth2.accessToken = 'my-access-token';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.query);
|
||||
sinon.assert.calledWithMatch(req.query, {'api_key': 'my-api-key'});
|
||||
sinon.assert.calledOnce(req.set);
|
||||
sinon.assert.calledWithMatch(req.set, {'Authorization': 'Bearer my-access-token'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
});
|
||||
|
||||
it('works when setting only api key', function() {
|
||||
apiKeyAuth.apiKey = 'my-api-key';
|
||||
oauth2.accessToken = null;
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.query);
|
||||
sinon.assert.calledWithMatch(req.query, {'api_key': 'my-api-key'});
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
});
|
||||
|
||||
it('works when neither api key nor access token is set', function() {
|
||||
apiKeyAuth.apiKey = null;
|
||||
oauth2.accessToken = null;
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.notCalled(req.query);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
});
|
||||
});
|
||||
|
||||
describe('unknown type', function() {
|
||||
var authName = 'unknown';
|
||||
var authNames = [authName];
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[authName] = {type: 'UNKNOWN'};
|
||||
});
|
||||
|
||||
it('throws error for unknown auth type', function() {
|
||||
expect(function() {
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
}).to.throwError();
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
describe('#defaultHeaders', function() {
|
||||
it('should initialize default headers to be an empty object', function() {
|
||||
expect(apiClient.defaultHeaders).to.eql({});
|
||||
});
|
||||
|
||||
it('should put default headers in request', function() {
|
||||
var newClient = new OpenAPIPetstore.ApiClient;
|
||||
newClient.defaultHeaders['Content-Type'] = 'text/plain'
|
||||
newClient.defaultHeaders['api_key'] = 'special-key'
|
||||
|
||||
var expected = {'Content-Type': 'text/plain', 'api_key': 'special-key'};
|
||||
expect(newClient.defaultHeaders).to.eql(expected);
|
||||
var req = makeDumbRequest(newClient);
|
||||
req.unset('User-Agent');
|
||||
expect(req.header).to.eql(expected);
|
||||
});
|
||||
|
||||
it('should override default headers with provided header params', function() {
|
||||
var newClient = new OpenAPIPetstore.ApiClient;
|
||||
newClient.defaultHeaders['Content-Type'] = 'text/plain'
|
||||
newClient.defaultHeaders['api_key'] = 'special-key'
|
||||
|
||||
var headerParams = {'Content-Type': 'application/json', 'Authorization': 'Bearer test-token'}
|
||||
var expected = {
|
||||
'Content-Type': 'application/json',
|
||||
'api_key': 'special-key',
|
||||
'Authorization': 'Bearer test-token'
|
||||
};
|
||||
var req = makeDumbRequest(newClient, {headerParams: headerParams});
|
||||
req.unset('User-Agent');
|
||||
expect(req.header).to.eql(expected);
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
});
|
||||
|
||||
function makeDumbRequest(apiClient, opts) {
|
||||
opts = opts || {};
|
||||
var path = opts.path || '/store/inventory';
|
||||
var httpMethod = opts.httpMethod || 'GET';
|
||||
var pathParams = opts.pathParams || {};
|
||||
var queryParams = opts.queryParams || {};
|
||||
var headerParams = opts.headerParams || {};
|
||||
var formParams = opts.formParams || {};
|
||||
var bodyParam = opts.bodyParam;
|
||||
var authNames = [];
|
||||
var contentTypes = opts.contentTypes || [];
|
||||
var accepts = opts.accepts || [];
|
||||
var callback = opts.callback;
|
||||
return apiClient.callApi(path, httpMethod, pathParams, queryParams,
|
||||
headerParams, formParams, bodyParam, authNames, contentTypes, accepts);
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
* OpenAPI Petstore
|
||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
|
||||
*
|
||||
* The version of the OpenAPI document: 1.0.0
|
||||
* OpenAPI spec version: 1.0.0
|
||||
*
|
||||
*
|
||||
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
|
||||
@ -14,10 +14,10 @@
|
||||
(function(root, factory) {
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
// AMD.
|
||||
define(['expect.js', process.cwd()+'/src/index'], factory);
|
||||
define(['expect.js', '../../src/index'], factory);
|
||||
} else if (typeof module === 'object' && module.exports) {
|
||||
// CommonJS-like environments that support module.exports, like Node.
|
||||
factory(require('expect.js'), require(process.cwd()+'/src/index'));
|
||||
factory(require('expect.js'), require('../../src/index'));
|
||||
} else {
|
||||
// Browser globals (root is window)
|
||||
factory(root.expect, root.OpenApiPetstore);
|
||||
@ -25,10 +25,10 @@
|
||||
}(this, function(expect, OpenApiPetstore) {
|
||||
'use strict';
|
||||
|
||||
var instance;
|
||||
var api_instance;
|
||||
|
||||
beforeEach(function() {
|
||||
instance = new OpenApiPetstore.PetApi();
|
||||
api_instance = new OpenApiPetstore.PetApi();
|
||||
});
|
||||
|
||||
var getProperty = function(object, getter, property) {
|
||||
@ -47,7 +47,56 @@
|
||||
object[property] = value;
|
||||
}
|
||||
|
||||
var createRandomPet = function() {
|
||||
var id = new Date().getTime();
|
||||
var pet = new OpenApiPetstore.Pet();
|
||||
setProperty(pet, "setId", "id", id);
|
||||
setProperty(pet, "setName", "name", "pet" + id);
|
||||
|
||||
var category = new OpenApiPetstore.Category();
|
||||
setProperty(category, "setId", "id", id);
|
||||
setProperty(category, "setName", "name", "category" + id);
|
||||
setProperty(pet, "setCategory", "category", category);
|
||||
|
||||
setProperty(pet, "setStatus", "status", "available");
|
||||
var photos = ["http://foo.bar.com/1", "http://foo.bar.com/2"];
|
||||
setProperty(pet, "setPhotoUrls", "photoUrls", photos);
|
||||
|
||||
return pet;
|
||||
}
|
||||
|
||||
describe('PetApi', function() {
|
||||
it('should create and get pet', function(done) {
|
||||
this.timeout(15000);
|
||||
var pet = createRandomPet();
|
||||
try {
|
||||
//api_instance.addPet(pet, {_base_path_index: 1}, function(error) {
|
||||
api_instance.addPet(pet, null, function(error) {
|
||||
if (error) throw error;
|
||||
|
||||
api_instance.getPetById(pet.id, function(error, fetched, response) {
|
||||
if (error) throw error;
|
||||
expect(response.status).to.be(200);
|
||||
expect(response.ok).to.be(true);
|
||||
expect(response.get('Content-Type')).to.be('application/json');
|
||||
|
||||
expect(fetched).to.be.a(OpenApiPetstore.Pet);
|
||||
expect(fetched.id).to.be(pet.id);
|
||||
expect(getProperty(fetched, "getPhotoUrls", "photoUrls"))
|
||||
.to.eql(getProperty(pet, "getPhotoUrls", "photoUrls"));
|
||||
expect(getProperty(fetched, "getCategory", "category"))
|
||||
.to.be.a(OpenApiPetstore.Category);
|
||||
expect(getProperty(getProperty(fetched, "getCategory", "category"), "getName", "name"))
|
||||
.to.be(getProperty(getProperty(pet, "getCategory", "category"), "getName", "name"));
|
||||
|
||||
api_instance.deletePet(pet.id);
|
||||
done();
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
done(error);
|
||||
}
|
||||
});
|
||||
describe('addPet', function() {
|
||||
it('should call addPet successfully', function(done) {
|
||||
//uncomment below and update the code to test addPet
|
||||
@ -128,16 +177,6 @@
|
||||
done();
|
||||
});
|
||||
});
|
||||
describe('uploadFileWithRequiredFile', function() {
|
||||
it('should call uploadFileWithRequiredFile successfully', function(done) {
|
||||
//uncomment below and update the code to test uploadFileWithRequiredFile
|
||||
//instance.uploadFileWithRequiredFile(function(error) {
|
||||
// if (error) throw error;
|
||||
//expect().to.be();
|
||||
//});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}));
|
||||
|
45
samples/client/petstore/javascript-es6/test/run_tests.html
Normal file
45
samples/client/petstore/javascript-es6/test/run_tests.html
Normal file
@ -0,0 +1,45 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Mocha Tests</title>
|
||||
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
|
||||
</head>
|
||||
<body>
|
||||
<div id="mocha"></div>
|
||||
|
||||
<script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script>
|
||||
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
|
||||
<script src="http://sinonjs.org/releases/sinon-1.17.3.js"></script>
|
||||
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
|
||||
<script>
|
||||
mocha.setup({
|
||||
ui: 'bdd',
|
||||
timeout: 10000
|
||||
});
|
||||
</script>
|
||||
|
||||
<script src="../node_modules/superagent/superagent.js"></script>
|
||||
|
||||
<script src="../src/ApiClient.js"></script>
|
||||
|
||||
<script src="../src/model/Category.js"></script>
|
||||
<script src="../src/model/Tag.js"></script>
|
||||
<script src="../src/model/InlineResponse200.js"></script>
|
||||
<script src="../src/model/Pet.js"></script>
|
||||
<script src="../src/model/Order.js"></script>
|
||||
<script src="../src/model/User.js"></script>
|
||||
|
||||
<script src="../src/api/PetApi.js"></script>
|
||||
<script src="../src/api/StoreApi.js"></script>
|
||||
|
||||
<script src="ApiClientTest.js"></script>
|
||||
<script src="api/PetApiTest.js"></script>
|
||||
<script src="api/StoreApiTest.js"></script>
|
||||
|
||||
<script>
|
||||
mocha.checkLeaks();
|
||||
mocha.globals(['jQuery']);
|
||||
mocha.run();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,394 @@
|
||||
if (typeof module === 'object' && module.exports) {
|
||||
var expect = require('expect.js');
|
||||
var SwaggerPetstore = require('../src/index');
|
||||
var sinon = require('sinon');
|
||||
}
|
||||
|
||||
var apiClient = SwaggerPetstore.ApiClient.instance;
|
||||
|
||||
describe('ApiClient', function() {
|
||||
describe('defaults', function() {
|
||||
it('should have correct default values with the default API client', function() {
|
||||
expect(apiClient).to.be.ok();
|
||||
expect(apiClient.basePath).to.be('http://petstore.swagger.io:80/v2');
|
||||
expect(apiClient.authentications).to.eql({
|
||||
petstore_auth: {type: 'oauth2'},
|
||||
bearer_test: {type: 'bearer'},
|
||||
http_basic_test: {type: 'basic'},
|
||||
api_key: {type: 'apiKey', 'in': 'header', name: 'api_key'},
|
||||
api_key_query: {type: 'apiKey', 'in': 'query', name: 'api_key_query'},
|
||||
/* comment out the following as these fake security def (testing purpose)
|
||||
* are removed from the spec, we'll add these back after updating the
|
||||
* petstore server
|
||||
*
|
||||
test_http_basic: {type: 'basic'},
|
||||
test_api_client_id: {
|
||||
type: 'apiKey',
|
||||
'in': 'header',
|
||||
name: 'x-test_api_client_id'
|
||||
},
|
||||
test_api_client_secret: {
|
||||
type: 'apiKey',
|
||||
'in': 'header',
|
||||
name: 'x-test_api_client_secret'
|
||||
},
|
||||
test_api_key_query: {
|
||||
type: 'apiKey',
|
||||
'in': 'query',
|
||||
name: 'test_api_key_query'
|
||||
},
|
||||
test_api_key_header: {
|
||||
type: 'apiKey',
|
||||
'in': 'header',
|
||||
name: 'test_api_key_header'
|
||||
}*/
|
||||
});
|
||||
});
|
||||
|
||||
it('should have correct default values with new API client and can customize it', function() {
|
||||
var newClient = new SwaggerPetstore.ApiClient;
|
||||
expect(newClient.basePath).to.be('http://petstore.swagger.io:80/v2');
|
||||
expect(newClient.buildUrl('/abc', {})).to.be('http://petstore.swagger.io:80/v2/abc');
|
||||
|
||||
newClient.basePath = 'http://example.com';
|
||||
expect(newClient.basePath).to.be('http://example.com');
|
||||
expect(newClient.buildUrl('/abc', {})).to.be('http://example.com/abc');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#paramToString', function() {
|
||||
it('should return empty string for null and undefined', function() {
|
||||
expect(apiClient.paramToString(null)).to.be('');
|
||||
expect(apiClient.paramToString(undefined)).to.be('');
|
||||
});
|
||||
|
||||
it('should return string', function() {
|
||||
expect(apiClient.paramToString('')).to.be('');
|
||||
expect(apiClient.paramToString('abc')).to.be('abc');
|
||||
expect(apiClient.paramToString(123)).to.be('123');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#buildCollectionParam', function() {
|
||||
var param;
|
||||
|
||||
beforeEach(function() {
|
||||
param = ['aa', 'bb', 123];
|
||||
});
|
||||
|
||||
it('works for csv', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'csv')).to.be('aa,bb,123');
|
||||
});
|
||||
|
||||
it('works for ssv', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'ssv')).to.be('aa bb 123');
|
||||
});
|
||||
|
||||
it('works for tsv', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'tsv')).to.be('aa\tbb\t123');
|
||||
});
|
||||
|
||||
it('works for pipes', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'pipes')).to.be('aa|bb|123');
|
||||
});
|
||||
|
||||
it('works for multi', function() {
|
||||
expect(apiClient.buildCollectionParam(param, 'multi')).to.eql(['aa', 'bb', '123']);
|
||||
});
|
||||
|
||||
it('fails for invalid collection format', function() {
|
||||
expect(function() { apiClient.buildCollectionParam(param, 'INVALID'); }).to.throwError();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#buildUrl', function() {
|
||||
it('should work without path parameters in the path', function() {
|
||||
expect(apiClient.buildUrl('/abc', {})).to
|
||||
.be('http://petstore.swagger.io:80/v2/abc');
|
||||
expect(apiClient.buildUrl('/abc/def?ok', {id: 123})).to
|
||||
.be('http://petstore.swagger.io:80/v2/abc/def?ok');
|
||||
});
|
||||
|
||||
it('should work with path parameters in the path', function() {
|
||||
expect(apiClient.buildUrl('/{id}', {id: 123})).to
|
||||
.be('http://petstore.swagger.io:80/v2/123');
|
||||
expect(apiClient.buildUrl('/abc/{id}/{name}?ok', {id: 456, name: 'a b'})).to.
|
||||
be('http://petstore.swagger.io:80/v2/abc/456/a%20b?ok');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#isJsonMime', function() {
|
||||
it('should return true for JSON MIME', function() {
|
||||
expect(apiClient.isJsonMime('application/json')).to.be(true);
|
||||
expect(apiClient.isJsonMime('application/json; charset=UTF8')).to.be(true);
|
||||
expect(apiClient.isJsonMime('APPLICATION/JSON')).to.be(true);
|
||||
});
|
||||
|
||||
it('should return false for non-JSON MIME', function() {
|
||||
expect(apiClient.isJsonMime('')).to.be(false);
|
||||
expect(apiClient.isJsonMime('text/plain')).to.be(false);
|
||||
expect(apiClient.isJsonMime('application/xml')).to.be(false);
|
||||
expect(apiClient.isJsonMime('application/jsonp')).to.be(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#applyAuthToRequest', function() {
|
||||
var req, newClient;
|
||||
|
||||
beforeEach(function() {
|
||||
req = {
|
||||
auth: function() {},
|
||||
set: function() {},
|
||||
query: function() {}
|
||||
};
|
||||
sinon.stub(req, 'auth');
|
||||
sinon.stub(req, 'set');
|
||||
sinon.stub(req, 'query');
|
||||
newClient = new SwaggerPetstore.ApiClient();
|
||||
});
|
||||
|
||||
describe('basic', function() {
|
||||
var authName = 'testBasicAuth';
|
||||
var authNames = [authName];
|
||||
var auth;
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[authName] = {type: 'basic'};
|
||||
auth = newClient.authentications[authName];
|
||||
});
|
||||
|
||||
it('sets auth header with username and password set', function() {
|
||||
auth.username = 'user';
|
||||
auth.password = 'pass';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.auth);
|
||||
// 'dXNlcjpwYXNz' is base64-encoded string of 'user:pass'
|
||||
sinon.assert.calledWithMatch(req.auth, 'user', 'pass');
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('sets header with only username set', function() {
|
||||
auth.username = 'user';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.auth);
|
||||
// 'dXNlcjo=' is base64-encoded string of 'user:'
|
||||
sinon.assert.calledWithMatch(req.auth, 'user', '');
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('sets header with only password set', function() {
|
||||
auth.password = 'pass';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.auth);
|
||||
// 'OnBhc3M=' is base64-encoded string of ':pass'
|
||||
sinon.assert.calledWithMatch(req.auth, '', 'pass');
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('does not set header when username and password are not set', function() {
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
});
|
||||
|
||||
describe('apiKey', function() {
|
||||
var authName = 'testApiKey';
|
||||
var authNames = [authName];
|
||||
var auth;
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[authName] = {type: 'apiKey', name: 'api_key'};
|
||||
auth = newClient.authentications[authName];
|
||||
});
|
||||
|
||||
it('sets api key in header', function() {
|
||||
auth.in = 'header';
|
||||
auth.apiKey = 'my-api-key';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.set);
|
||||
sinon.assert.calledWithMatch(req.set, {'api_key': 'my-api-key'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('sets api key in query', function() {
|
||||
auth.in = 'query';
|
||||
auth.apiKey = 'my-api-key';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.query);
|
||||
sinon.assert.calledWithMatch(req.query, {'api_key': 'my-api-key'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
});
|
||||
|
||||
it('sets api key in header with prefix', function() {
|
||||
auth.in = 'header';
|
||||
auth.apiKey = 'my-api-key';
|
||||
auth.apiKeyPrefix = 'Key';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.set);
|
||||
sinon.assert.calledWithMatch(req.set, {'api_key': 'Key my-api-key'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('works when api key is not set', function() {
|
||||
auth.in = 'query';
|
||||
auth.apiKey = null;
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.notCalled(req.query);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
});
|
||||
});
|
||||
|
||||
describe('oauth2', function() {
|
||||
var authName = 'testOAuth2';
|
||||
var authNames = [authName];
|
||||
var auth;
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[authName] = {type: 'oauth2'};
|
||||
auth = newClient.authentications[authName];
|
||||
});
|
||||
|
||||
it('sets access token in header', function() {
|
||||
auth.accessToken = 'my-access-token';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.set);
|
||||
sinon.assert.calledWithMatch(req.set, {'Authorization': 'Bearer my-access-token'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
|
||||
it('works when access token is not set', function() {
|
||||
auth.accessToken = null;
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.notCalled(req.query);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
});
|
||||
});
|
||||
|
||||
describe('apiKey and oauth2', function() {
|
||||
var apiKeyAuthName = 'testApiKey';
|
||||
var oauth2Name = 'testOAuth2';
|
||||
var authNames = [apiKeyAuthName, oauth2Name];
|
||||
var apiKeyAuth, oauth2;
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[apiKeyAuthName] = {type: 'apiKey', name: 'api_key', 'in': 'query'};
|
||||
newClient.authentications[oauth2Name] = {type: 'oauth2'};
|
||||
apiKeyAuth = newClient.authentications[apiKeyAuthName];
|
||||
oauth2 = newClient.authentications[oauth2Name];
|
||||
});
|
||||
|
||||
it('works when setting both api key and access token', function() {
|
||||
apiKeyAuth.apiKey = 'my-api-key';
|
||||
oauth2.accessToken = 'my-access-token';
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.query);
|
||||
sinon.assert.calledWithMatch(req.query, {'api_key': 'my-api-key'});
|
||||
sinon.assert.calledOnce(req.set);
|
||||
sinon.assert.calledWithMatch(req.set, {'Authorization': 'Bearer my-access-token'});
|
||||
sinon.assert.notCalled(req.auth);
|
||||
});
|
||||
|
||||
it('works when setting only api key', function() {
|
||||
apiKeyAuth.apiKey = 'my-api-key';
|
||||
oauth2.accessToken = null;
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.calledOnce(req.query);
|
||||
sinon.assert.calledWithMatch(req.query, {'api_key': 'my-api-key'});
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
});
|
||||
|
||||
it('works when neither api key nor access token is set', function() {
|
||||
apiKeyAuth.apiKey = null;
|
||||
oauth2.accessToken = null;
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
sinon.assert.notCalled(req.query);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.set);
|
||||
});
|
||||
});
|
||||
|
||||
describe('unknown type', function() {
|
||||
var authName = 'unknown';
|
||||
var authNames = [authName];
|
||||
|
||||
beforeEach(function() {
|
||||
newClient.authentications[authName] = {type: 'UNKNOWN'};
|
||||
});
|
||||
|
||||
it('throws error for unknown auth type', function() {
|
||||
expect(function() {
|
||||
newClient.applyAuthToRequest(req, authNames);
|
||||
}).to.throwError();
|
||||
sinon.assert.notCalled(req.set);
|
||||
sinon.assert.notCalled(req.auth);
|
||||
sinon.assert.notCalled(req.query);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
describe('#defaultHeaders', function() {
|
||||
it('should initialize default headers to be an empty object', function() {
|
||||
expect(apiClient.defaultHeaders).to.eql({});
|
||||
});
|
||||
|
||||
it('should put default headers in request', function() {
|
||||
var newClient = new SwaggerPetstore.ApiClient;
|
||||
newClient.defaultHeaders['Content-Type'] = 'text/plain'
|
||||
newClient.defaultHeaders['api_key'] = 'special-key'
|
||||
|
||||
var expected = {'Content-Type': 'text/plain', 'api_key': 'special-key'};
|
||||
expect(newClient.defaultHeaders).to.eql(expected);
|
||||
var req = makeDumbRequest(newClient);
|
||||
req.unset('User-Agent');
|
||||
expect(req.header).to.eql(expected);
|
||||
});
|
||||
|
||||
it('should override default headers with provided header params', function() {
|
||||
var newClient = new SwaggerPetstore.ApiClient;
|
||||
newClient.defaultHeaders['Content-Type'] = 'text/plain'
|
||||
newClient.defaultHeaders['api_key'] = 'special-key'
|
||||
|
||||
var headerParams = {'Content-Type': 'application/json', 'Authorization': 'Bearer test-token'}
|
||||
var expected = {
|
||||
'Content-Type': 'application/json',
|
||||
'api_key': 'special-key',
|
||||
'Authorization': 'Bearer test-token'
|
||||
};
|
||||
var req = makeDumbRequest(newClient, {headerParams: headerParams});
|
||||
req.unset('User-Agent');
|
||||
expect(req.header).to.eql(expected);
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
});
|
||||
|
||||
function makeDumbRequest(apiClient, opts) {
|
||||
opts = opts || {};
|
||||
var path = opts.path || '/store/inventory';
|
||||
var httpMethod = opts.httpMethod || 'GET';
|
||||
var pathParams = opts.pathParams || {};
|
||||
var queryParams = opts.queryParams || {};
|
||||
var headerParams = opts.headerParams || {};
|
||||
var formParams = opts.formParams || {};
|
||||
var bodyParam = opts.bodyParam;
|
||||
var authNames = [];
|
||||
var contentTypes = opts.contentTypes || [];
|
||||
var accepts = opts.accepts || [];
|
||||
var callback = opts.callback;
|
||||
return apiClient.callApi(path, httpMethod, pathParams, queryParams,
|
||||
headerParams, formParams, bodyParam, authNames, contentTypes, accepts);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user