question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Polly.JS doesn't encode multipart/form-data requests properly

See original GitHub issue

Description

This is a bug report.

Polly.JS appears to not send all of a multipart/form-data request to servers when it captures the initial server response. Most servers return an error when they cannot parse multipart form boundaries in a request.

Update: Polly.JS doesn’t pass multipart/form-data bodies with the right encoding. See comments below.

Shareable Source

I created a minimal reproduction here that uses request to upload a file to a simple express server: https://github.com/swashcap/pollyjs-multipart-form-data-error

Error Message & Stack Trace

Most servers return an error when they cannot parse multipart form boundaries in a request. Polly.JS emits an error similar to:

PollyError: [Polly] [persister:fs] Cannot persist response for [GET] http://localhost:3001/ because the status code was 400 and `recordFailedRequests` is `false`

Config

I’m using setup-polly-jest to run tests in Node.js. Here’s the config:

const AdapterNodeHTTP = require('@pollyjs/adapter-node-http')
const PersisterFS = require('@pollyjs/persister-fs')
const path = require('path')
const { Polly } = require('@pollyjs/core')
const { setupPolly } = require('setup-polly-jest')

Polly.register(AdapterNodeHTTP)
Polly.register(PersisterFS)

// ...

describe('this great test', () => {
  const context = setupPolly({
    adapters: ['node-http'],
    persister: 'fs'
  })

  // ...
})

(see the test here: https://github.com/swashcap/pollyjs-multipart-form-data-error/blob/master/src/upload.spec.js)

Dependencies

$ yarn list --pattern=polly --silent
├─ @pollyjs/adapter-node-http@2.6.2
├─ @pollyjs/adapter@2.6.0
├─ @pollyjs/core@2.6.2
├─ @pollyjs/node-server@2.6.0
├─ @pollyjs/persister-fs@2.6.2
├─ @pollyjs/persister@2.6.2
├─ @pollyjs/utils@2.6.0
└─ setup-polly-jest@0.5.2

Environment

$ node -e "var os=require('os');console.log('Node.js ' + process.version + '\n' + os.platform() + ' ' + os.release())"
Node.js v10.16.3
darwin 18.7.0
$ npm --version
6.9.0
$ yarn --version
1.17.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jasonmitcommented, Sep 17, 2019

This should actually be a pretty small fix; shall I open a PR?

Yes please 😃

0reactions
swashcapcommented, Sep 17, 2019

First off, thanks for diving into this.

No problem! We’re enjoying Polly.JS quite a bit 🙂

What do y’all think of a patch in Polly.JS that checks whether body is all hex right here?

Is the goal to avoid calling JSON.stringify on body if it’s all hex values?

The goal would be to correct the body’s encoding when it’s sent to the upstream server in HttpAdapter#passthroughRequest. Right now it’s hex, it should be decoded to utf8.

This should actually be a pretty small fix; shall I open a PR?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch API and multer error while uploading file - Stack Overflow
js server (I'm using github's pollyfill if it has something to do with it: https://github.com/github/fetch). The request is done like this:
Read more >
[Solved]-Cannot get Polly to work with Collapse Request-C#
Coding example for the question Cannot get Polly to work with Collapse Request-C#.
Read more >
Polly.JS - GitHub
Standalone, framework-agnostic JavaScript library that enables recording, replaying, and stubbing HTTP interactions.
Read more >
File Upload in ASP.NET Core 6 - Detailed Guide
In this approach, the file is uploaded in a multipart request and ... forms must specify an encoding type (enctype) as multipart/form-data.
Read more >
S3 — Boto3 Docs 1.26.38 documentation - Amazon AWS
The parts list must be specified in order by part number. 400 Bad Request. Error code: NoSuchUpload. Description: The specified multipart upload does...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found