Skip to content
Merged

Sign #59

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test/config.js
.idea/
demo.js
.nyc_output/
.DS_Store
12 changes: 11 additions & 1 deletion lib/roa.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ function filter(value) {
return value.replace(/[\t\n\r\f]/g, ' ');
}

function keyLowerify(headers) {
const keys = Object.keys(headers);
const newHeaders = {};
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
newHeaders[key.toLowerCase()] = headers[key];
}
return newHeaders;
}

function getCanonicalizedHeaders(headers) {
const prefix = 'x-acs-';
const keys = Object.keys(headers);
Expand Down Expand Up @@ -154,7 +164,7 @@ class ROAClient {
request(method, uriPattern, query = {}, body = '', headers = {}, opts = {}) {
var postBody = null;

var mixHeaders = Object.assign(this.buildHeaders(), headers);
var mixHeaders = Object.assign(this.buildHeaders(), keyLowerify(headers));
if (body) {
postBody = Buffer.from(body, 'utf8');
mixHeaders['content-md5'] = kitx.md5(postBody, 'base64');
Expand Down
29 changes: 29 additions & 0 deletions test/roa.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,32 @@ describe('roa request', function () {
expect(result).to.be.a('string');
});
});

describe('nlp', function () {
var client = new ROAClient({
accessKeyId: process.env.ACCESS_KEY_ID,
accessKeySecret: process.env.ACCESS_KEY_SECRET,
endpoint: 'http://nlp.cn-shanghai.aliyuncs.com',
apiVersion: '2018-04-08',
});

it('translate should ok', async function () {
const params = {
q: '你好',
source: 'zh',
target: 'en',
format: 'text',
};

const res = await client.request(
'POST',
'/nlp/api/translate/standard',
{},
JSON.stringify(params),
{ 'Content-Type': 'application/json' }
);

expect(res).to.be.ok();
expect(res.data).to.be.ok();
});
});
7 changes: 7 additions & 0 deletions test/roa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,13 @@ describe('roa core', function() {
expect(filter('he\f\fllo')).to.be('he llo');
});

it('keyLowerify should ok', function () {
const keyLowerify = roa.__get__('keyLowerify');
expect(keyLowerify({})).to.be.eql({});
expect(keyLowerify({'low': 'value'})).to.be.eql({'low': 'value'});
expect(keyLowerify({'Low': 'value'})).to.be.eql({'low': 'value'});
});

it('parseXML should ok', async function () {
const parseXML = roa.__get__('parseXML');
try {
Expand Down