Skip to content

Commit

Permalink
feat: support Intellij Multiline FormUrlEncoded Syntax (#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed May 30, 2024
1 parent d4ad16c commit 8d345cd
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fix
- prevent readFile Error log output on package.json search (Anweber/vscode-httpyac#293)
- disabled MetaData Handler checks disabled State on occurence of line in execution order (AnWeber/vscode-httpyac#292)
- support Intellij Multiline FormUrlEncoded Syntax (#699)

## [6.13.3]
### Fix
Expand Down
1 change: 1 addition & 0 deletions src/plugins/core/registerCorePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function initOnRequestHook(api: models.HttpyacHooksApi) {
api.hooks.onRequest.addHook('resolveRequestBody', request.resolveRequestBody);
api.hooks.onRequest.addHook('requestVariableReplacer', request.requestVariableReplacer);
api.hooks.onRequest.addHook('transformRequestBody', request.transformRequestBodyToBuffer);
api.hooks.onRequest.addHook('transfromMultilineFormUrlEncoded', request.transfromMultilineFormUrlEncoded);
api.hooks.onRequest.addHook('encodeRequestBody', request.encodeRequestBody);

api.hooks.onRequest.addInterceptor(request.isTrustedInterceptor);
Expand Down
28 changes: 28 additions & 0 deletions src/plugins/core/request/encodeRequestBody.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { encodeRequestBody } from './encodeRequestBody';
import { Request } from '../../../models';
describe('encodeRequestBody', () => {
it('encodeRequestBody should return encoded string', () => {
const request = {
body: 'a b',
contentType: {
mimeType: 'application/x-www-form-urlencoded',
},
} as Request;
encodeRequestBody(request);
expect(request.body).toBe('a%20b');
});
it('encodeRequestBody should not transform if not valid mimeType', () => {
const request = {
body: 'a b',
} as Request;
encodeRequestBody(request);
expect(request.body).toBe('a b');
});
it('encodeRequestBody should not transform if is buffer', () => {
const request = {
body: Buffer.from('a b'),
} as Request;
encodeRequestBody(request);
expect(request.body).toBeInstanceOf(Buffer);
});
});
8 changes: 2 additions & 6 deletions src/plugins/core/request/encodeRequestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import * as models from '../../../models';
import * as utils from '../../../utils';

export async function encodeRequestBody(request: models.Request): Promise<void> {
if (request.body) {
if (utils.isString(request.body)) {
if (utils.isMimeTypeFormUrlEncoded(request.contentType)) {
request.body = encodeUrl(request.body);
}
}
if (request.body && utils.isString(request.body) && utils.isMimeTypeFormUrlEncoded(request.contentType)) {
request.body = encodeUrl(request.body);
}
}
1 change: 1 addition & 0 deletions src/plugins/core/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './requestVariableReplacer';
export * from './resolveRequestBody';
export * from './setEnvRequestOptions';
export * from './transformRequestBodyToBuffer';
export * from './transfromMultilineFormUrlEncoded';
21 changes: 21 additions & 0 deletions src/plugins/core/request/transfromMultilineFormUrlEncoded.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { transfromMultilineFormUrlEncoded } from './transfromMultilineFormUrlEncoded';
import { Request } from '../../../models';
describe('transfromMultilineFormUrlEncoded', () => {
it('transfromMultilineFormUrlEncoded should return joined string', () => {
const request = {
body: 'foo = a&\nbar = b',
contentType: {
mimeType: 'application/x-www-form-urlencoded',
},
} as Request;
transfromMultilineFormUrlEncoded(request);
expect(request.body).toBe('foo=a&bar=b');
});
it('transfromMultilineFormUrlEncoded should ignore content on wrong mimetype', () => {
const request = {
body: 'foo = a&\nbar = b',
} as Request;
transfromMultilineFormUrlEncoded(request);
expect(request.body).toBe('foo = a&\nbar = b');
});
});
21 changes: 21 additions & 0 deletions src/plugins/core/request/transfromMultilineFormUrlEncoded.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as models from '../../../models';
import * as utils from '../../../utils';

export async function transfromMultilineFormUrlEncoded(request: models.Request): Promise<void> {
if (request.body && utils.isString(request.body) && utils.isMimeTypeFormUrlEncoded(request.contentType)) {
const lines = utils.toMultiLineArray(request.body);
if (lines.length > 0) {
const result = [];

for (const line of lines) {
result.push(
line
.split('=')
.map(t => t.trim())
.join('=')
);
}
request.body = result.join('');
}
}
}

0 comments on commit 8d345cd

Please sign in to comment.