forked from loafle/openapi-generator-original
Merge pull request #1947 from xhh/javascript-default-headers
[JavaScript] Add default headers and some tests
This commit is contained in:
commit
ca941b1e3c
@ -16,7 +16,15 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ApiClient = function ApiClient() {
|
var ApiClient = function ApiClient() {
|
||||||
|
/**
|
||||||
|
* The base path to put in front of every API call's (relative) path.
|
||||||
|
*/
|
||||||
this.basePath = '{{basePath}}'.replace(/\/+$/, '');
|
this.basePath = '{{basePath}}'.replace(/\/+$/, '');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default HTTP headers to be included for all API calls.
|
||||||
|
*/
|
||||||
|
this.defaultHeaders = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
ApiClient.prototype.paramToString = function paramToString(param) {
|
ApiClient.prototype.paramToString = function paramToString(param) {
|
||||||
@ -76,6 +84,26 @@
|
|||||||
return mimes[0];
|
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:
|
* Normalize parameters values:
|
||||||
* remove nils,
|
* remove nils,
|
||||||
@ -87,7 +115,7 @@
|
|||||||
for (var key in params) {
|
for (var key in params) {
|
||||||
if (params.hasOwnProperty(key) && params[key] != null) {
|
if (params.hasOwnProperty(key) && params[key] != null) {
|
||||||
var value = params[key];
|
var value = params[key];
|
||||||
if (value instanceof Blob || Array.isArray(value)) {
|
if (this.isFileParam(value) || Array.isArray(value)) {
|
||||||
newParams[key] = value;
|
newParams[key] = value;
|
||||||
} else {
|
} else {
|
||||||
newParams[key] = this.paramToString(value);
|
newParams[key] = this.paramToString(value);
|
||||||
@ -107,10 +135,14 @@
|
|||||||
request.query(this.normalizeParams(queryParams));
|
request.query(this.normalizeParams(queryParams));
|
||||||
|
|
||||||
// set header parameters
|
// set header parameters
|
||||||
request.set(this.normalizeParams(headerParams));
|
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
|
||||||
|
|
||||||
var contentType = this.jsonPreferredMime(contentTypes) || 'application/json';
|
var contentType = this.jsonPreferredMime(contentTypes);
|
||||||
request.type(contentType);
|
if (contentType) {
|
||||||
|
request.type(contentType);
|
||||||
|
} else if (!request.header['Content-Type']) {
|
||||||
|
request.type('application/json');
|
||||||
|
}
|
||||||
|
|
||||||
if (contentType === 'application/x-www-form-urlencoded') {
|
if (contentType === 'application/x-www-form-urlencoded') {
|
||||||
request.send(this.normalizeParams(formParams));
|
request.send(this.normalizeParams(formParams));
|
||||||
@ -118,7 +150,7 @@
|
|||||||
var _formParams = this.normalizeParams(formParams);
|
var _formParams = this.normalizeParams(formParams);
|
||||||
for (var key in _formParams) {
|
for (var key in _formParams) {
|
||||||
if (_formParams.hasOwnProperty(key)) {
|
if (_formParams.hasOwnProperty(key)) {
|
||||||
if (_formParams[key] instanceof Blob) {
|
if (this.isFileParam(_formParams[key])) {
|
||||||
// file field
|
// file field
|
||||||
request.attach(key, _formParams[key]);
|
request.attach(key, _formParams[key]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
"test": "./node_modules/mocha/bin/mocha --recursive"
|
"test": "./node_modules/mocha/bin/mocha --recursive"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"superagent": "^1.6.1"
|
"superagent": "^1.7.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "~2.3.4",
|
"mocha": "~2.3.4",
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
"test": "./node_modules/mocha/bin/mocha --recursive"
|
"test": "./node_modules/mocha/bin/mocha --recursive"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"superagent": "^1.6.1"
|
"superagent": "^1.7.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "~2.3.4",
|
"mocha": "~2.3.4",
|
||||||
|
@ -16,7 +16,15 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var ApiClient = function ApiClient() {
|
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(/\/+$/, '');
|
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) {
|
ApiClient.prototype.paramToString = function paramToString(param) {
|
||||||
@ -76,6 +84,26 @@
|
|||||||
return mimes[0];
|
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:
|
* Normalize parameters values:
|
||||||
* remove nils,
|
* remove nils,
|
||||||
@ -87,7 +115,7 @@
|
|||||||
for (var key in params) {
|
for (var key in params) {
|
||||||
if (params.hasOwnProperty(key) && params[key] != null) {
|
if (params.hasOwnProperty(key) && params[key] != null) {
|
||||||
var value = params[key];
|
var value = params[key];
|
||||||
if (value instanceof Blob || Array.isArray(value)) {
|
if (this.isFileParam(value) || Array.isArray(value)) {
|
||||||
newParams[key] = value;
|
newParams[key] = value;
|
||||||
} else {
|
} else {
|
||||||
newParams[key] = this.paramToString(value);
|
newParams[key] = this.paramToString(value);
|
||||||
@ -107,10 +135,14 @@
|
|||||||
request.query(this.normalizeParams(queryParams));
|
request.query(this.normalizeParams(queryParams));
|
||||||
|
|
||||||
// set header parameters
|
// set header parameters
|
||||||
request.set(this.normalizeParams(headerParams));
|
request.set(this.defaultHeaders).set(this.normalizeParams(headerParams));
|
||||||
|
|
||||||
var contentType = this.jsonPreferredMime(contentTypes) || 'application/json';
|
var contentType = this.jsonPreferredMime(contentTypes);
|
||||||
request.type(contentType);
|
if (contentType) {
|
||||||
|
request.type(contentType);
|
||||||
|
} else if (!request.header['Content-Type']) {
|
||||||
|
request.type('application/json');
|
||||||
|
}
|
||||||
|
|
||||||
if (contentType === 'application/x-www-form-urlencoded') {
|
if (contentType === 'application/x-www-form-urlencoded') {
|
||||||
request.send(this.normalizeParams(formParams));
|
request.send(this.normalizeParams(formParams));
|
||||||
@ -118,7 +150,7 @@
|
|||||||
var _formParams = this.normalizeParams(formParams);
|
var _formParams = this.normalizeParams(formParams);
|
||||||
for (var key in _formParams) {
|
for (var key in _formParams) {
|
||||||
if (_formParams.hasOwnProperty(key)) {
|
if (_formParams.hasOwnProperty(key)) {
|
||||||
if (_formParams[key] instanceof Blob) {
|
if (this.isFileParam(_formParams[key])) {
|
||||||
// file field
|
// file field
|
||||||
request.attach(key, _formParams[key]);
|
request.attach(key, _formParams[key]);
|
||||||
} else {
|
} else {
|
||||||
|
@ -66,4 +66,55 @@ describe('ApiClient', function() {
|
|||||||
expect(apiClient.isJsonMime('application/jsonp')).to.be(false);
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user