Mocking Http request type

Hello,

How do you go about creating a mock for the following?

req = http.request("http://example.hashicorp.com")
  .with_header("Content-Type", "application/json")
  .with_body(json.marshal({ "foo": "bar" }))

Mocking http request function on it’s own is not an issue, but what should it return so that you can properly mock the “request” type?

Hi @jcbfaulks ,

Thanks for your question and welcome to the Sentinel forum!

Mocking the http import is a difficult task, but it can be done (to an extent). I’ll share a playground example of how I have managed to mock it. Each function duplicates itself and recreates the appropriate functions with the current request state.

https://play.sentinelproject.io/p/tiK7H0rM9rd

Let me know if it works for you!

Thanks @cam-stitt, that works great!

1 Like

@jcbfaulks Good to hear! You should be able to further extend that mock with any other required function mocks.