ijpiantanida / talkback

A simple HTTP proxy that records and playbacks requests
MIT License
284 stars 41 forks source link

UTF-8 characters in response returns malformed data #23

Closed rmtsrc closed 5 years ago

rmtsrc commented 5 years ago

For example having the following setup and tapes:

index.js

const talkback = require("talkback");

const opts = {
  host: "https://example.com",
};
const server = talkback(opts);
server.start(() => console.log("Talkback Started"));

tapes/quotes.json5

{
    meta: {
        createdAt: '2019-03-19T22:03:41.814Z',
        host: 'https://example.com',
        resHumanReadable: true,
    },
    req: {
        url: '/test.json',
        method: 'GET',
        headers: {
            accept: '*/*',
            'user-agent': 'curl',
        },
        body: '',
    },
    res: {
        status: 200,
        headers: {
            'content-type': [
                'application/json; charset=UTF-8',
            ],
            date: [
                'Tue, 19 Mar 2019 22:03:42 GMT',
            ],
            expires: [
                'Tue, 26 Mar 2019 22:03:42 GMT',
            ],
            'last-modified': [
                'Tue, 19 Mar 2019 22:00:27 GMT',
            ],
            'content-length': [
                '46',
            ],
            connection: [
                'close',
            ],
        },
        body: {
            words: 'in special “quotes” break',
        },
    },
}

node index.js

curl -v -H 'User-Agent: curl' http://localhost:8080/test.json

Returns

* Excess found in a non pipelined read: excess = 4, size = 42, maxdownload = 42, bytecount = 0

{
  "words": "in special “quotes” brea

tapes/emojis.json5

{
    meta: {
        createdAt: '2019-03-19T22:03:41.814Z',
        host: 'https://example.com',
        resHumanReadable: true,
    },
    req: {
        url: '/test2.json',
        method: 'GET',
        headers: {
            accept: '*/*',
            'user-agent': 'curl',
        },
        body: '',
    },
    res: {
        status: 200,
        headers: {
            'content-type': [
                'application/json; charset=UTF-8',
            ],
            date: [
                'Tue, 19 Mar 2019 22:03:42 GMT',
            ],
            expires: [
                'Tue, 26 Mar 2019 22:03:42 GMT',
            ],
            'last-modified': [
                'Tue, 19 Mar 2019 22:00:27 GMT',
            ],
            'content-length': [
                '33',
            ],
            connection: [
                'close',
            ],
        },
        body: {
            emojis: 'also 😢 break',
        },
    },
}

curl -v -H 'User-Agent: curl' http://localhost:8080/test2.json

Returns

* Excess found in a non pipelined read: excess = 2, size = 31, maxdownload = 31, bytecount = 0

{
  "emojis": "also 😢 break"

Notice how the response is malformed and missing the closing brackets and objects.

A workaround is to remove the response content-length.

ijpiantanida commented 5 years ago

Hi @sebflipper, I just released v1.10.0 which includes this fix. Thanks for the contribution.