Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

formdata filter #2366

Merged
merged 1 commit into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions runtime/ms-rest/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2.3.0 (12/18/2017)
- Added support for processing formData parameters by setting the `"form"` property on the request object when the `'Content-Type'` header is `'application/x-www-form-urlencoded'`. This needs to be done since we depend on the [request library](https://github.com/request/request#applicationx-www-form-urlencoded-url-encoded-forms).

### 2.2.9 (12/15/2017)
- Runtime now populates polymorphic discriminator value if it is missing in both serialization and deserialization.

Expand Down Expand Up @@ -31,6 +34,7 @@
### 2.2.0 (4/29/2017)
- Minor bug fix in `WebResource.prepare()` while processing query parameters.
- Removed native references to `Buffer.isBuffer()` and stream and replaced it with packages that are browser compatible.

### 2.1.0 (4/14/2017)
- Ensured `'use strict';` is applied correctly in all the files #2131
- Modified the handling of `Content-Type` request header in `Webresource.prepare()` #2126
Expand Down
89 changes: 52 additions & 37 deletions runtime/ms-rest/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,44 @@

const Constants = {
/**
* Specifies HTTP.
*
* @const
* @type {string}
*/
* Specifies HTTP.
*
* @const
* @type {string}
*/
HTTP: 'http:',

/**
* Specifies HTTPS.
*
* @const
* @type {string}
*/
* Specifies HTTPS.
*
* @const
* @type {string}
*/
HTTPS: 'https:',

/**
* Specifies HTTP Proxy.
*
* @const
* @type {string}
*/
* Specifies HTTP Proxy.
*
* @const
* @type {string}
*/
HTTP_PROXY: 'HTTP_PROXY',

/**
* Specifies HTTPS Proxy.
*
* @const
* @type {string}
*/
* Specifies HTTPS Proxy.
*
* @const
* @type {string}
*/
HTTPS_PROXY: 'HTTPS_PROXY',

HttpConstants: {
/**
* Http Verbs
*
* @const
* @enum {string}
*/
* Http Verbs
*
* @const
* @enum {string}
*/
HttpVerbs: {
PUT: 'PUT',
GET: 'GET',
Expand All @@ -55,26 +55,41 @@ const Constants = {
},

/**
* Defines constants for use with HTTP headers.
*/
* Defines constants for use with HTTP headers.
*/
HeaderConstants: {
/**
* The Authorization header.
*
* @const
* @type {string}
*/
* The Authorization header.
*
* @const
* @type {string}
*/
AUTHORIZATION: 'authorization',

/**
* The Authorization scheme.
*
* @const
* @type {string}
*/
AUTHORIZATION_SCHEME: 'Bearer',

/**
* The UserAgent header.
*
* @const
* @type {string}
*/
* The UserAgent header.
*
* @const
* @type {string}
*/
USER_AGENT: 'user-agent',

/**
* The 'application/x-www-form-urlencoded' Content-Type header.
*
* @const
* @type {string}
*/
FORM_URL_ENCODED: 'application/x-www-form-urlencoded',

}
};

Expand Down
26 changes: 26 additions & 0 deletions runtime/ms-rest/lib/filters/formDataFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

'use strict';

const HeaderConstants = require('../constants').HeaderConstants;
/**
* formDataFilter - This filter is resposnsible for setting the form property on the
* request object if the Content-Type header is 'application/x-www-form-urlencoded'
*/
exports.create = function () {
return function handle (resource, next, callback) {
if (resource && resource.headers) {
let headers = resource.headers;
let contentType = headers['Content-Type'] || headers['content-type'];
if (contentType === HeaderConstants.FORM_URL_ENCODED && resource.formData) {
let form = resource.formData;
resource.form = form;
delete resource.formData;
}
}
return next(resource, function (err, response, body) {
return callback(err, response, body);
});
};
};
1 change: 1 addition & 0 deletions runtime/ms-rest/lib/msRest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports.LogFilter = require('./filters/logFilter');
exports.SigningFilter = require('./filters/signingFilter');
exports.ExponentialRetryPolicyFilter = require('./filters/exponentialRetryPolicyFilter');
exports.UserAgentFilter = require('./filters/msRestUserAgentFilter');
exports.FormDataFilter = require('./filters/formDataFilter');

exports.requestPipeline = require('./requestPipeline');
exports.stripResponse = utils.stripResponse;
Expand Down
5 changes: 4 additions & 1 deletion runtime/ms-rest/lib/serviceClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const ProxyFilter = require('./filters/proxyFilter');
const UserAgentFilter = require('./filters/msRestUserAgentFilter');
const RedirectFilter = require('./filters/redirectFilter');
const SigningFilter = require('./filters/signingFilter');
const FormDataFilter = require('./filters/formDataFilter');
const ExponentialRetryPolicyFilter = require('./filters/exponentialRetryPolicyFilter');
const SystemErrorRetryPolicyFilter = require('./filters/systemErrorRetryPolicyFilter');
const requestPipeline = require('./requestPipeline');
Expand Down Expand Up @@ -157,9 +158,11 @@ class ServiceClient {
if (credentials) {
options.filters.push(SigningFilter.create(credentials));
}


options.filters.push(FormDataFilter.create());
options.filters.push(UserAgentFilter.create(this.userAgentInfo.value));
options.filters.push(RedirectFilter.create());

if (!options.noRetryPolicy) {
options.filters.push(new ExponentialRetryPolicyFilter());
options.filters.push(new SystemErrorRetryPolicyFilter());
Expand Down
2 changes: 1 addition & 1 deletion runtime/ms-rest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion runtime/ms-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"email": "azsdkteam@microsoft.com",
"url": "https://github.com/Azure/azure-sdk-for-node"
},
"version": "2.2.9",
"version": "2.3.0",
"description": "Client Runtime for Node.js client libraries generated using AutoRest",
"tags": [
"node",
Expand Down
66 changes: 66 additions & 0 deletions runtime/ms-rest/test/formDataFilterTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

var assert = require('assert');
var url = require('url');

var FormDataFilter = require('../lib/filters/formDataFilter');
var HeaderConstants = require('../lib/constants').HeaderConstants;

describe.only('FormData filter', function () {
it('should set the form property of the request object when content-type is application/x-www-form-urlencoded', function (done) {
let request = {
url: 'https://foo.example.com/v1/pet/puppy?api-version=2017-12-01',
method: 'POST',
formData: {
id: 101,
name: 'myPuppy',
color: 'red'
},
headers:{
'Content-Type': HeaderConstants.FORM_URL_ENCODED
}
};
var callback = function () { done(); };
var mocknext = function (err, response, body) {
let form = {
id: 101,
name: 'myPuppy',
color: 'red'
};
assert.equal(request.formData, undefined);
assert.deepEqual(request.form, form);
return callback();
};
var formDataFilter = FormDataFilter.create();
formDataFilter(request, mocknext, callback);
});

it('should NOT set the form property of the request object when content-type is multipart/form-data', function (done) {
let request = {
url: 'https://foo.example.com/v1/pet/puppy?api-version=2017-12-01',
method: 'POST',
formData: {
id: 101,
name: 'myPuppy',
color: 'red'
},
headers:{
'Content-Type': 'multipart/form-data'
}
};
var callback = function () { done(); };
var mocknext = function (err, response, body) {
let formData = {
id: 101,
name: 'myPuppy',
color: 'red'
};
assert.equal(request.form, undefined);
assert.deepEqual(request.formData, formData);
return callback();
};
var formDataFilter = FormDataFilter.create();
formDataFilter(request, mocknext, callback);
});
});
3 changes: 2 additions & 1 deletion runtime/ms-rest/test/testlist.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
serializationTests
formdataFilterTests.js
serializationTests.js
credentialTests.js
retryPolicyTests.js
proxyFilterTests.js
Expand Down