bernardolins/fake_server

Add request_received

Closed this issue · 2 comments

It will be nice to have a macro where the user could describe a request the server must receive so the test case passes. Something like:

test_with_server "user update parameters" do
  route "/users/save", Response.no_content!
  
  assert :ok == User.save()

  assert request_received "/users/save", 
    method: :put, 
    body: "name=new_name&email=new_email@test.com",
    headers: %{"authorization": "bearer mytoken"} ,
    count: 1
end

The options could be headers, body, method and any other related thing (maybe all fields of the Request structure). This macro must ensure the given route exists and return true if the requests made during the test match the given parameters. The code above must be equivalent to the following:

test_with_server "user update parameters" do
  route "/users/save", fn(%Request{body: body, headers: headers, method: method}) ->
     if body == "name=new_name&email=new_email@test.com" 
       && headers == headers: %{"authorization": "bearer mytoken"}
       && method == :put,  
     do: Response.no_content!, else: Response.bad_request!
  end
  assert :ok == User.save()
  assert hits("/users/save") == 1
end

I'll work in this Issue.

The method I added is slightly different from what was requested :

    assert request_received "/users/save",
      method: "PUT",
      body: "name=new_name&email=new_email@test.com",
      headers: %{"authorization" => "bearer mytoken"} ,
      count: 1

It was easier to work with cowboy and Httpoison if the values contained no atoms.
This is why the method parameter is now a String and the headers parameter is a map of Strings.