elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 85 forks source link

Wrap conn(:get, "/") |> make_response #26

Closed dblock closed 8 years ago

dblock commented 8 years ago

This is a little cumbersome to spell:

assert %Plug.Conn{resp_body: "It works!"} = conn(:get, "/") |> make_response

Maybe it can be more Ruby-like? We'd want to write

assert %Plug.Conn{resp_body: "It works!"} = get("/")

Now this is my first day writing Elixir, but I'd be happy to give it a shot if anyone thinks it's a good idea.

falood commented 8 years ago

Obviously, get("/") is better. But I don't know how to design DSL for requests with custom header or body. So I built it like this, it's the same with plug and everyone knows plug can use it easily. I'll optimize it step by step.

I can't tell you how excited I am for your attention. I copied DSL of grape exactly before maru v0.8.x, but I can't going to do it by reason of difference between ruby and elixir, they're quite different language. And then I almost totally rewrite it. Leave me a message at any time if there's any question or suggestion about maru ! Thank you for your awesome grape, again!

dblock commented 8 years ago

We'll figure it out!

leejarvis commented 8 years ago

I think you could wrap this by adding a get/2 into Maru.Test?

def get(path, version \\ nil) do
  Plug.Test.conn(:get, path) |> make_response(version)
end

Maybe it would be useful for Maru.Test to actually build on this and include some basic functions that wrap around Plug.Conn like Phoenix does? For example:

test "returning json" do
  conn = get("/users")
  assert json_response(conn) == [%{"name" => "lee"}]
end

test "returning text" do
  conn = get("/other")
  assert text_response(conn) == "hello, world"
end
dblock commented 8 years ago

@leejarvis This looks closer to what I'd want. PR!

falood commented 8 years ago

@leejarvis @dblock What should I do if I need a custom header or post body as format of form_data ?

leejarvis commented 8 years ago

In that case it might be worth considering again doing something similar what Phoenix does. They build a %Plug.Conn and then use that to form the request, e.g.:

test "returning text" do
  conn = build_conn()
  conn = conn.put_req_header(conn, "Authorization", "something")
  conn = get(conn, "/other")
  assert text_response(conn) == "hello, world"
end

Which would allow you to use the setup tags quite nicely too:

setup(tags) do
  tags = Map.put(tags, :conn, build_conn())
  {:ok, tags}
end

test "returning text", %{conn: conn} do
  response = conn
  |> put_req_header("Authorisation", "something")
  |> get("/")
  |> text_response

  assert response == "hello, world"
end

Just a couple of ideas, anyway.

falood commented 8 years ago

@leejarvis Thank you! It looks good, I'll try to do something like this.

falood commented 8 years ago

Documents have been updated here.

dblock commented 8 years ago

I think this can be closed?

falood commented 8 years ago

Yeah.

leejarvis commented 8 years ago

Nice work!