Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parses empty GET request body as undefined #179

Merged
merged 4 commits into from
May 25, 2020

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented May 22, 2020

Changes

  • Treats only GET requests with empty body as undefined during the parsing of the message received from the service worker.

The captured request's req.body is an empty string ("") even if not set.

  • Removes unused type definitions.
  • Adds integration tests for GET request with no body.

GitHub

@kettanaito
Copy link
Member Author

kettanaito commented May 22, 2020

Experiencing this exception when running test/rest/body.test.ts:

Protocol error (Network.getResponseBody): No data found for resource with given identifier

      19 |           url: `${test.origin}/get-body`,
      20 |         })
    > 21 |         const body = await res.text()
         |                                ^
      22 |
      23 |         expect(body).toBeUndefined()
      24 |       })

      at Promise (../node_modules/puppeteer/lib/Connection.js:154:63)
      at CDPSession.send (../node_modules/puppeteer/lib/Connection.js:153:16)
      at _contentPromise._bodyLoadedPromise.then (../node_modules/puppeteer/lib/NetworkManager.js:557:53)
        -- ASYNC --
      at Response.<anonymous> (../node_modules/puppeteer/lib/helper.js:82:19)
      at rest-api/body.test.ts:21:32
      at fulfilled (rest-api/body.test.ts:5:58)

Based on this Puppeteer issue this may mean that the navigation happened and destroyed the pending request, however, I don't think that's the case. I've also tried issuing a test request to a different path (/get-body), which produced the exact exception (not the "same path" issue).

Most likely has something to do with those requests/handlers. It's peculiar, because both GET and POST requests with no and empty ("") body respectively, fail, while they produce a different mocked response (in case of GET req.body: undefined, when POST req.body: '').

@kettanaito
Copy link
Member Author

The No data found for resource with given identifier error is caused by the mocked response having body: undefined when attempting to call await res.text() (the same for res.buffer). It means that either Puppeteer or an HTTP spec somehow forbids setting an explicit undefined or an empty string as a response body. I don't think it's likely, but it's what's happening.

When the ctx.text() has a fallback value to a non-empty string, res.text() works as expected.

@kettanaito
Copy link
Member Author

Resolution

There is an issue with Puppeteer when calling res.text on a response that has undefined or "" as its body. To circumvent it, I've made the test request handler always return a JSON body, where a key of that body equals the actual req.body.

Setting undefined or "" as ctx.text() is still perfectly valid, it just doesn't work with the current testing setup.

@kettanaito kettanaito merged commit 310218b into master May 25, 2020
@kettanaito kettanaito deleted the 175-req-empty-body-parse branch May 25, 2020 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty request JSON body throws exception in a request handler
1 participant