Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Handle Content-Type headers with a charset #30

Merged
merged 1 commit into from
Feb 20, 2018
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: 2 additions & 2 deletions lib/grpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ function makeServer(fn) {
const { payload } = message;
const headers = MessageHeaders.fromObject(message.headers);

// TODO case insensitive headers
const contentType = headers.getValue('Content-Type') || 'text/plain';
// TODO correctly handle content-type charset, instead of ignoring it
const contentType = (headers.getValue('Content-Type') || 'text/plain').split(';')[0].trim();
const accept = headers.getValue('Accept') || 'text/plain';
const correlationId = headers.getValue('correlationId');

Expand Down
52 changes: 52 additions & 0 deletions spec/grpcSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,58 @@ describe('grpc', () => {
call.end();
});

it('should handle a content-type charset', done => {
const call = client.call();
const onData = jasmine.createSpy('onData');
const onEnd = () => {
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenCalledWith('riff');

expect(onData).toHaveBeenCalledTimes(1);
const { headers, payload } = parseMessage(onData.calls.first().args[0]);
expect(headers.getValues('Content-Type')).toEqual(['text/plain']);
expect(payload.toString()).toBe('riff');

done();
};
call.on('data', onData);
call.on('end', onEnd);
call.write(
new MessageBuilder()
.addHeader('Accept', 'text/plain')
.addHeader('Content-Type', 'text/plain; charset=utf-8')
.payload('riff')
.build()
);
call.end();
});

it('should handle compound accept types', done => {
const call = client.call();
const onData = jasmine.createSpy('onData');
const onEnd = () => {
expect(fn).toHaveBeenCalledTimes(1);
expect(fn).toHaveBeenCalledWith('riff');

expect(onData).toHaveBeenCalledTimes(1);
const { headers, payload } = parseMessage(onData.calls.first().args[0]);
expect(headers.getValues('Content-Type')).toEqual(['text/plain']);
expect(payload.toString()).toBe('riff');

done();
};
call.on('data', onData);
call.on('end', onEnd);
call.write(
new MessageBuilder()
.addHeader('Accept', 'application/json;q=0.5, text/plain')
.addHeader('Content-Type', 'text/plain')
.payload('riff')
.build()
);
call.end();
});

it('should reject unsupported content types', done => {
const call = client.call();
const onData = jasmine.createSpy('onData');
Expand Down