bruno icon indicating copy to clipboard operation
bruno copied to clipboard

Add dataBuffer to response to be available in test scripts

Open RainoPikkarainen opened this issue 1 year ago • 6 comments

Description

I have a use case for accessing the dataBuffer in tests to validate a binary response from DNS over https services. Since the dataBuffer was already exposed some time ago for displaying preview images or such, these trivial changes made it possible to access the binary buffer in tests to parse DNS packets.

image image image

I have been able to easily merge these changes and build the app locally, but now my local build does not have the new Golden key stuff, so it would be nice to have this or similar in the main branch.

Use case

meta {
  name: cloudflare-dns-query
  type: http
  seq: 6
}

get {
  url: https://cloudflare-dns.com/dns-query?dns=6bABAAABAAAAAAAAA3d3dxBnb29nbGVhZHNlcnZpY2VzA2NvbQAAAQAB
  body: none
  auth: none
}

query {
  dns: 6bABAAABAAAAAAAAA3d3dxBnb29nbGVhZHNlcnZpY2VzA2NvbQAAAQAB
  ~name: www.googleadservices.com
  ~type: A
}

headers {
  Accept: application/dns-message
  ~Accept: application/dns-json
}

tests {
  const DnsPacket = require('@dnsquery/dns-packet');
  
  const buffer = Buffer.from(res.getDataBuffer());
  const dnsPacket1 = DnsPacket.decode(buffer);
  console.log(JSON.stringify(dnsPacket1, null, 4));
  
  
  test("should get at least one answer", function() {
    expect(dnsPacket1.answers.length).to.be.greaterThan(0);
  });
  
  test("should get answer name matching query", function() {
    expect(dnsPacket1.answers.pop().name).to.equal("www.googleadservices.com");
  });
  
}

Contribution Checklist:

  • [x] The pull request only addresses one issue or adds one feature.
  • [x] The pull request does not introduce any breaking changes
  • [x] I have added screenshots or gifs to help explain the change if applicable.
  • [x] I have read the contribution guidelines.
  • [x] Create an issue and link to the pull request.

RainoPikkarainen avatar Mar 21 '24 09:03 RainoPikkarainen

This also needs to be added to CLI: run-single-request.js

Its-treason avatar Mar 21 '24 16:03 Its-treason

run-single-request.js doesn't seem to do the data = JSON.parse(response.data) stuff, so response.data is the buffer type already. Please help me out here if there needs to be some API compatibility.

RainoPikkarainen avatar Mar 21 '24 16:03 RainoPikkarainen

run-single-request.js doesn't seem to do the data = JSON.parse(response.data) stuff, so response.data is the buffer type already. Please help me out here if there needs to be some API compatibility.

Yes, but this may also be another problem, if it's not the same in electron & CLI. But I can't test this right now, I look into it on the weekend.

Its-treason avatar Mar 21 '24 18:03 Its-treason