Merge pull request #1947 from xhh/javascript-default-headers

[JavaScript] Add default headers and some tests
This commit is contained in:
wing328 2016-01-26 12:40:29 +08:00
commit ca941b1e3c
5 changed files with 127 additions and 12 deletions

View File

@ -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 {

View File

@ -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",

View File

@ -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",

View File

@ -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 {

View File

@ -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
);
}