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

Bug: Pre-request script is executed before the request is prepared #2249

Open
2 tasks done
pietrygamat opened this issue May 6, 2024 · 3 comments · May be fixed by #2250
Open
2 tasks done

Bug: Pre-request script is executed before the request is prepared #2249

pietrygamat opened this issue May 6, 2024 · 3 comments · May be fixed by #2250
Labels
bug Something isn't working

Comments

@pietrygamat
Copy link
Contributor

I have checked the following:

  • I use the newest version of bruno.
  • I've searched existing issues and found nothing related to my issue.

Describe the bug

When executing the request, bruno goes through several stages of configuration, among them: execution o pre-request script, post-request script, and run of 'configureRequest` method. The current execution order is:

  • execute pre-request scripts
  • configure request (interpolate vars, sets proxy, auth headers. cookies)
  • execute request
  • execute post-request

I wonder if it would make more sense to execute the steps in this order:

  • configure request
  • execute-pre-request script
  • execute request
  • execute post-request script

This way any logic built into bruno may be overwritten by user's script and on top of that, user's script execute on request that is closer to what is actually being sent (e.g. with variables resolved, headers set).

For example, the below bru file logs this to console:

Before
{{api_url}}
{"Content-Type":"application/json"}

After
https://localhost:8083
{"Content-Type":"application/json","Authorization":"Basic dXNlcjpwYXNzd29yZA=="}

while I would expect the output Before and After to be the same.

.bru file to reproduce the bug

meta {
name: example
type: http
}

post {
url: {{api_url}}
body: text
auth: basic
}

headers {
Content-Type: application/json
}

auth:basic {
username: user
password: password
}

script:pre-request {
console.log('Before');
console.log(req.url);
console.log(JSON.stringify(req.getHeaders()));
}

script:post-response {
console.log('After');
console.log(req.url);
console.log(JSON.stringify(req.getHeaders()));
}

Screenshots/Live demo link

N/A

@pietrygamat pietrygamat added the bug Something isn't working label May 6, 2024
pietrygamat pushed a commit to pietrygamat/bruno that referenced this issue May 6, 2024
The pre-request script phase is now executed after configureRequest. That means the
'req' object in pre-request script has access to more data, including evaluated variables, set authorization headers, etc.

fixes usebruno#2249
@lohxt1
Copy link
Collaborator

lohxt1 commented May 6, 2024

Hey @pietrygamat

We have a fix in place for this, which is planned to be included in the upcoming release.

@centur
Copy link

centur commented May 14, 2024

Wow :(
This bug broke heaps of my existing scripts, I've to find a older bruno version and get back to it, as I'm suddenly blocked .
@lohxt1 I wonder if these types of scripts still possible after the fix.

console.log(`createTenant`)
const { readFileSync } = require('fs');
const { URL } = require('url');
const path = require('path');

var attachmentFilename ="../../my-config.zip"
const attachmentPath = path.join(bru.cwd(), attachmentFilename);

console.log(attachmentPath);

const attachment = readFileSync(attachmentPath);
const attachmentLength = attachment.length;

req.setHeader("Content-Type", "application/zip");
req.setHeader("Content-Length", attachmentLength);
req.setBody(attachment);

var randomName = faker.location.city()
bru.setVar("configName", `bruno-${randomName}`);

For the context - because bruno does not support raw file (and I want some extra control over file selection) I've implemented this script to send raw binary content in post request. few versions ago it was fully functional script, now it's not working at all. Not sure what exactly was broken but errors or console logs from pre-req are not delivered anywhere and this file is not being sent to api eiter
And FYI - console.log in pre-request ceased to work, nothing shows in console even if request is trivial

Bruno version: 1.17.0
MacOS: 14.4.1 (23E224)

@pietrygamat
Copy link
Contributor Author

@lohxt1 do you have an update on this one, or a public branch where this can be peeked at?

We have a fix in place for this, which is planned to be included in the upcoming release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants