PSPDFKit-labs / bypass

Bypass provides a quick way to create a custom plug that can be put in place instead of an actual HTTP server to return prebaked responses to client requests.
https://hex.pm/packages/bypass
MIT License
964 stars 111 forks source link

Add 'headers' param in expect and expect_once function #107

Closed nicogoblin closed 3 years ago

nicogoblin commented 4 years ago

Hi there, I want to propose a modify for the following method: expect and expect_once. Because I find not so easy and clear do an assertion on headers request. For example, I use this method to check the request header during my last work.

      Bypass.expect_once(
        bypass,
        "GET",
        "/some/path/to/test",
        fn conn ->
          assert Plug.Conn.get_req_header(conn, "header_name") == [
                   "header_value"
                 ]

          Plug.Conn.resp(conn, 200, ~s<{"data": {
              "some_data": "data_value"
              }}>)
        end
      )

I think that a method like this is more useful and clear:

      Bypass.expect_once(
        bypass,
        "GET",
        "/some/path/to/test",
        [{"header_name", "header_value"}],
        &Plug.Conn.resp(&1, 200, ~s<{"data": {
              "some_data": "data_value"
              }}>)
      )

functions: expect(bypass, method, path, headers, fun)

expect_once(bypass, method, path, headers, fun)

headers is a list of the tuple and the function check if every tuple in the list is present in the request.

I want to listen to your opinion about this pr proposal. In case you agree, I would be happy to do it! 😃

ream88 commented 3 years ago

I would not make the API more complex tbh. If this is is a helper you need often, you could maybe extract them to a seperate hex package!