playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[BUG] Requests don't preserve the HTTP method on redirects

Open gskierk opened this issue 2 years ago • 1 comments

Source code

  • [x] I provided exact source code that allows reproducing the issue locally.

Server

// server.js

const express = require('express')

const app = express()

app.post('/test', (request, response) => {
  if (request.get('host').includes('localhost')) { // or any other condition...
    response.redirect('http://127.0.0.1:3000/test')
  } else {
    response.json({ method: 'post' })
  }
})

app.get('/test', (_, response) => {
  response.json({ method: 'get' })
})

app.listen(3000, () => {
  console.log('Server is listening...')
})

Client

// client.mjs

import { request } from '@playwright/test'

const requestContext = await request.newContext()
const response = await requestContext.post('http://localhost:3000/test')

const json = await response.json()

console.log({ json })

Steps

  • Start the demo server node server.js.
  • When the server is running, start the client node client.mjs.

Expected

{ json: { method: 'post' } } should be printed to the console.

Actual

{ json: { method: 'get' } } is being printed to the console.

Comment

This particular case could be handled by the client automatically or an additional option could be added. For example, Postman has Follow original HTTP Method option in settings.

gskierk avatar May 19 '23 12:05 gskierk

@gskierk APIRequestContext follows the fetch spec, step 12. I'd recommend to pass maxRedirects: 0, and then follow the redirect manually.

dgozman avatar May 19 '23 18:05 dgozman

@dgozman It sounds reasonable enough to me. Also, I can confirm that handling it manually works without any issues.

gskierk avatar May 20 '23 20:05 gskierk