Skip to content

Commit

Permalink
solving the #17 problem...
Browse files Browse the repository at this point in the history
something wrong. couldn't pass testcodes
  • Loading branch information
유용우 / CX committed Jul 29, 2019
1 parent 36c629d commit 7910b78
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
103 changes: 103 additions & 0 deletions src/__tests__/common.utils.escape-quote.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { AxiosRequestConfig } from 'axios';
import debug from 'debug';
import * as shelljs from 'shelljs';
// tslint:disable-next-line:import-name
import r2curl from '../index';
import { defaultR2CurlOptions } from '../interface/IR2CurlOptions';
import CommonUtils from '../lib/CommonUtils';

const log = debug('r2curl:test:escape-quote');

describe('Escape Quote Test', () => {
test('simple single quote', done => {
CommonUtils.bootstrap({ ...defaultR2CurlOptions, quote: 'single' });
const text = 'Hello \'World\'';
const escaped = CommonUtils.escapeQuote(text);

log(escaped);

expect(escaped).toBe('Hello \\\'World\\\'');

done();
});
test('simple double quote', done => {
CommonUtils.bootstrap({ ...defaultR2CurlOptions, quote: 'double' });
const text = 'Hello "World"';
const escaped = CommonUtils.escapeQuote(text);

log(escaped);

expect(escaped).toBe('Hello \\"World\\"');

done();
});
test('single object stringify', done => {
CommonUtils.bootstrap({ ...defaultR2CurlOptions, quote: 'single' });
const payload = {
text1: 'Hello \'World\'',
text2: 'I\'m Gro\'ot',
't\'est': '\'\'\'\'\'',
};
const escaped = CommonUtils.jsonStringifyWithEscapeQuote(payload);

expect(escaped).toBe(`{"text1":"Hello \\'World\\'","text2":"I\\'m Gro\\'ot","t\\'est":"\\'\\'\\'\\'\\'"}`);

log(escaped);
done();
});
test('r2curl command with single quote in body', done => {
const config: AxiosRequestConfig = {
url: 'https://google.com',
method: 'POST',
data: {
introduce: 'I\'m Yowu',
caller: 'https://github.com/uyu423/r2curl',
sorry: true,
},
headers: {
'content-Type': 'application/json',
},
};

const curl = r2curl(config);

log(curl);

expect(curl).toBe(
// tslint:disable-next-line: max-line-length
'curl -X POST \'https://google.com\' -H \'content-Type:application/json\' --data \'{"caller":"https://github.com/uyu423/r2curl","sorry":true}\'',
);

const exec = shelljs.exec(`${curl} --silent > /dev/null`);
expect(exec.code).toBeLessThan(1);
done();
});
test('r2curl command with double quote in body', done => {
const config: AxiosRequestConfig = {
url: 'https://google.com',
method: 'POST',
data: {
introduce: 'My Name Is "Yu Yongwoo"',
caller: 'https://github.com/uyu423/r2curl',
sorry: true,
},
headers: {
'content-Type': 'application/json',
},
};

const curl = r2curl(config, { quote: 'double' });

log(curl);

expect(curl).toBe(
// tslint:disable-next-line: max-line-length
'curl -X POST \'https://google.com\' -H \'content-Type:application/json\' --data \'{"caller":"https://github.com/uyu423/r2curl","sorry":true}\'',
);

const exec = shelljs.exec(`${curl} --silent > /dev/null`);

expect(exec.code).toBeLessThan(1);
done();
});
});
5 changes: 4 additions & 1 deletion src/lib/BodyHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HTTP_HEADER_LOWERCASE } from '../enum/HTTP_HEADER';
import { HTTP_HEADER_CONTENT_TYPE } from '../enum/HTTP_HEADER_CONTENT_TYPE';
import CommonUtils from './CommonUtils';
import { isEmpty, isNotEmpty } from './isEmpty';

export class BodyHelper {
Expand Down Expand Up @@ -59,6 +60,8 @@ export class BodyHelper {
.join('&');
}
private getTextBody(): string | null {
return typeof this._rawBody === 'object' || Array.isArray(this._rawBody) ? JSON.stringify(this._rawBody) : null;
return typeof this._rawBody === 'object' || Array.isArray(this._rawBody)
? CommonUtils.jsonStringifyWithEscapeQuote(this._rawBody)
: null;
}
}
14 changes: 14 additions & 0 deletions src/lib/CommonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,19 @@ export default class CommonUtils {
return `${this.quote}${content}${this.quote}`;
}

/**
* If the same quota is present in a quoted string, it will not be properly parsed and added.
*
* @see https://github.com/uyu423/r2curl/issues/17
*/
public static escapeQuote(content: string): string {
const regex = new RegExp(this.quote, 'gi');
return content.replace(regex, `\\${this.quote}`);
}

public static jsonStringifyWithEscapeQuote(payload: any): string {
return this.escapeQuote(JSON.stringify(payload));
}

private static quote: '\'' | '"';
}

0 comments on commit 7910b78

Please sign in to comment.