From 794589d6b8a33c8a8c346430f0619c9a3fc1fe39 Mon Sep 17 00:00:00 2001 From: xhh Date: Fri, 22 Jan 2016 18:37:51 +0800 Subject: [PATCH] JavaScript: add default headers and some tests --- .../resources/Javascript/ApiClient.mustache | 42 +++++++++++++-- .../resources/Javascript/package.mustache | 2 +- .../client/petstore/javascript/package.json | 2 +- .../petstore/javascript/src/ApiClient.js | 42 +++++++++++++-- .../petstore/javascript/test/ApiClientTest.js | 51 +++++++++++++++++++ 5 files changed, 127 insertions(+), 12 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache index 84d07d6933b..006d8d50eb7 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache @@ -16,7 +16,15 @@ 'use strict'; var ApiClient = function ApiClient() { + /** + * The base path to put in front of every API call's (relative) path. + */ this.basePath = '{{basePath}}'.replace(/\/+$/, ''); + + /** + * The default HTTP headers to be included for all API calls. + */ + this.defaultHeaders = {}; }; ApiClient.prototype.paramToString = function paramToString(param) { @@ -76,6 +84,26 @@ return mimes[0]; }; + /** + * Check if the given parameter value is like file content. + */ + ApiClient.prototype.isFileParam = function isFileParam(param) { + // Buffer or fs.ReadStream in Node.js + if (typeof module === 'object' && module.exports && + (param instanceof Buffer || param instanceof require('fs').ReadStream)) { + return true; + } + // Blob in browser + if (typeof Blob === 'function' && param instanceof Blob) { + return true; + } + // File in browser (it seems File object is also instance of Blob, but keep this for safe) + if (typeof File === 'function' && param instanceof File) { + return true; + } + return false; + }; + /** * Normalize parameters values: * remove nils, @@ -87,7 +115,7 @@ for (var key in params) { if (params.hasOwnProperty(key) && params[key] != null) { var value = params[key]; - if (value instanceof Blob || Array.isArray(value)) { + if (this.isFileParam(value) || Array.isArray(value)) { newParams[key] = value; } else { newParams[key] = this.paramToString(value); @@ -107,10 +135,14 @@ request.query(this.normalizeParams(queryParams)); // set header parameters - request.set(this.normalizeParams(headerParams)); + request.set(this.defaultHeaders).set(this.normalizeParams(headerParams)); - var contentType = this.jsonPreferredMime(contentTypes) || 'application/json'; - request.type(contentType); + var contentType = this.jsonPreferredMime(contentTypes); + if (contentType) { + request.type(contentType); + } else if (!request.header['Content-Type']) { + request.type('application/json'); + } if (contentType === 'application/x-www-form-urlencoded') { request.send(this.normalizeParams(formParams)); @@ -118,7 +150,7 @@ var _formParams = this.normalizeParams(formParams); for (var key in _formParams) { if (_formParams.hasOwnProperty(key)) { - if (_formParams[key] instanceof Blob) { + if (this.isFileParam(_formParams[key])) { // file field request.attach(key, _formParams[key]); } else { diff --git a/modules/swagger-codegen/src/main/resources/Javascript/package.mustache b/modules/swagger-codegen/src/main/resources/Javascript/package.mustache index 1e7650a1ba1..b60c95835b5 100644 --- a/modules/swagger-codegen/src/main/resources/Javascript/package.mustache +++ b/modules/swagger-codegen/src/main/resources/Javascript/package.mustache @@ -8,7 +8,7 @@ "test": "./node_modules/mocha/bin/mocha --recursive" }, "dependencies": { - "superagent": "^1.6.1" + "superagent": "^1.7.1" }, "devDependencies": { "mocha": "~2.3.4", diff --git a/samples/client/petstore/javascript/package.json b/samples/client/petstore/javascript/package.json index fe470da8b27..99b71097721 100644 --- a/samples/client/petstore/javascript/package.json +++ b/samples/client/petstore/javascript/package.json @@ -8,7 +8,7 @@ "test": "./node_modules/mocha/bin/mocha --recursive" }, "dependencies": { - "superagent": "^1.6.1" + "superagent": "^1.7.1" }, "devDependencies": { "mocha": "~2.3.4", diff --git a/samples/client/petstore/javascript/src/ApiClient.js b/samples/client/petstore/javascript/src/ApiClient.js index 3476b7dbba4..7fdc0ca81dc 100644 --- a/samples/client/petstore/javascript/src/ApiClient.js +++ b/samples/client/petstore/javascript/src/ApiClient.js @@ -16,7 +16,15 @@ 'use strict'; var ApiClient = function ApiClient() { + /** + * The base path to put in front of every API call's (relative) path. + */ this.basePath = 'http://petstore.swagger.io/v2'.replace(/\/+$/, ''); + + /** + * The default HTTP headers to be included for all API calls. + */ + this.defaultHeaders = {}; }; ApiClient.prototype.paramToString = function paramToString(param) { @@ -76,6 +84,26 @@ return mimes[0]; }; + /** + * Check if the given parameter value is like file content. + */ + ApiClient.prototype.isFileParam = function isFileParam(param) { + // Buffer or fs.ReadStream in Node.js + if (typeof module === 'object' && module.exports && + (param instanceof Buffer || param instanceof require('fs').ReadStream)) { + return true; + } + // Blob in browser + if (typeof Blob === 'function' && param instanceof Blob) { + return true; + } + // File in browser (it seems File object is also instance of Blob, but keep this for safe) + if (typeof File === 'function' && param instanceof File) { + return true; + } + return false; + }; + /** * Normalize parameters values: * remove nils, @@ -87,7 +115,7 @@ for (var key in params) { if (params.hasOwnProperty(key) && params[key] != null) { var value = params[key]; - if (value instanceof Blob || Array.isArray(value)) { + if (this.isFileParam(value) || Array.isArray(value)) { newParams[key] = value; } else { newParams[key] = this.paramToString(value); @@ -107,10 +135,14 @@ request.query(this.normalizeParams(queryParams)); // set header parameters - request.set(this.normalizeParams(headerParams)); + request.set(this.defaultHeaders).set(this.normalizeParams(headerParams)); - var contentType = this.jsonPreferredMime(contentTypes) || 'application/json'; - request.type(contentType); + var contentType = this.jsonPreferredMime(contentTypes); + if (contentType) { + request.type(contentType); + } else if (!request.header['Content-Type']) { + request.type('application/json'); + } if (contentType === 'application/x-www-form-urlencoded') { request.send(this.normalizeParams(formParams)); @@ -118,7 +150,7 @@ var _formParams = this.normalizeParams(formParams); for (var key in _formParams) { if (_formParams.hasOwnProperty(key)) { - if (_formParams[key] instanceof Blob) { + if (this.isFileParam(_formParams[key])) { // file field request.attach(key, _formParams[key]); } else { diff --git a/samples/client/petstore/javascript/test/ApiClientTest.js b/samples/client/petstore/javascript/test/ApiClientTest.js index f577a3ec7b6..daae849bcf1 100644 --- a/samples/client/petstore/javascript/test/ApiClientTest.js +++ b/samples/client/petstore/javascript/test/ApiClientTest.js @@ -66,4 +66,55 @@ describe('ApiClient', function() { expect(apiClient.isJsonMime('application/jsonp')).to.be(false); }); }); + + 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 contentTypes = opts.contentTypes || []; + var accepts = opts.accepts || []; + var callback = opts.callback; + return apiClient.callApi(path, httpMethod, pathParams, queryParams, + headerParams, formParams, bodyParam, contentTypes, accepts, callback + ); +}