Skip to content

Commit

Permalink
Merge 02a73e6 into a7c9e72
Browse files Browse the repository at this point in the history
  • Loading branch information
elvanja committed Nov 28, 2019
2 parents a7c9e72 + 02a73e6 commit 5ad3b48
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -133,8 +133,8 @@ test "list comments for post", %{conn: conn} do
insert_posts_with_comments()

conn = conn
|> get(comments_path(conn, :index)
|> BlueBird.ConnLogger.save()
|> get(comments_path(conn, :index))
|> BlueBird.ConnLogger.save(title: "Without params")

assert json_response(conn, 200)
end
Expand Down
6 changes: 4 additions & 2 deletions lib/blue_bird/conn_logger.ex
Expand Up @@ -75,8 +75,10 @@ defmodule BlueBird.ConnLogger do
iex> save(conn)
:ok
"""
@spec save(Plug.Conn.t()) :: :ok
def save(conn) do
@type save_option :: {:title, String.t()}
@spec save(Plug.Conn.t(), [save_option()]) :: Plug.Conn.t()
def save(conn, opts \\ []) do
conn = Plug.Conn.assign(conn, :blue_bird_opts, opts)
GenServer.cast(__MODULE__, {:save, conn})
conn
end
Expand Down
3 changes: 3 additions & 0 deletions lib/blue_bird/generator.ex
Expand Up @@ -212,9 +212,12 @@ defmodule BlueBird.Generator do

@spec request_map(%PhxRoute{}, %Plug.Conn{}) :: Request.t()
defp request_map(route, conn) do
opts = conn.assigns[:blue_bird_opts]

%Request{
method: conn.method,
path: route.path,
title: Keyword.get(opts, :title),
headers: filter_headers(conn.req_headers, :request),
path_params: conn.path_params,
body_params: conn.body_params,
Expand Down
8 changes: 5 additions & 3 deletions lib/blue_bird/request.ex
Expand Up @@ -6,6 +6,7 @@ defmodule BlueBird.Request do
:method,
:path,
:response,
:title,
path_params: %{},
body_params: %{},
headers: [],
Expand All @@ -18,10 +19,11 @@ defmodule BlueBird.Request do
@type t :: %BlueBird.Request{
method: String.t(),
path: String.t(),
headers: [{String.t(), String.t()}],
response: BlueBird.Response.t(),
title: String.t(),
path_params: %{optional(String.t()) => String.t()},
body_params: %{optional(String.t()) => String.t()},
query_params: %{optional(String.t()) => String.t()},
response: BlueBird.Response.t()
headers: [{String.t(), String.t()}],
query_params: %{optional(String.t()) => String.t()}
}
end
3 changes: 2 additions & 1 deletion lib/blue_bird/writer/blueprint.ex
Expand Up @@ -132,6 +132,7 @@ defmodule BlueBird.Writer.Blueprint do
end

defp process_request(request) do
title = request.title || request.response.status
content_type = get_content_type(request.headers)

req_str =
Expand All @@ -146,7 +147,7 @@ defmodule BlueBird.Writer.Blueprint do
if req_str == "" && content_type == "" do
""
else
"+ Request #{request.response.status}#{content_type}\n\n" <>
"+ Request #{title}#{content_type}\n\n" <>
req_str <> "\n"
end
end
Expand Down
29 changes: 22 additions & 7 deletions test/conn_logger_test.exs
@@ -1,21 +1,36 @@
defmodule BlueBird.Test.ConnLoggerTest do
use ExUnit.Case
use BlueBird.Test.Support.ConnCase

alias BlueBird.ConnLogger

@conn_1 %{mary: "jane"}
@conn_2 %{jane: "austen"}

test "ConnLogger saves, returns and resets connections" do
ConnLogger.reset()

ConnLogger.save(@conn_1)
ConnLogger.save(@conn_2)
ConnLogger.save(build_conn(:get, "/read/mary_jane"))
ConnLogger.save(build_conn(:get, "/read/jane_austen"))

conns = ConnLogger.get_conns()

assert ConnLogger.get_conns() == [@conn_1, @conn_2]
assert conns
|> Enum.map(& &1.request_path)
|> Enum.sort() == ["/read/jane_austen", "/read/mary_jane"]

ConnLogger.reset()

assert ConnLogger.get_conns() == []
end

test "accepts additional options as :blue_bird_opts" do
ConnLogger.reset()

ConnLogger.save(build_conn(:get, "/read/jane_austen"),
title: "No pride and no prejudice"
)

[conn] = ConnLogger.get_conns()

assert conn.assigns == %{
blue_bird_opts: [title: "No pride and no prejudice"]
}
end
end
1 change: 1 addition & 0 deletions test/formatter_test.exs
Expand Up @@ -3,6 +3,7 @@ defmodule BlueBird.Test.FormatterTest do

alias BlueBird.Formatter

@tag :capture_log
test "Formatter runs Generator and Writer" do
path_apib = Path.join(["priv", "static", "docs", "api.apib"])
# path_swagger = Path.join(["priv", "static", "docs", "swagger.json"])
Expand Down
13 changes: 13 additions & 0 deletions test/generator_test.exs
Expand Up @@ -267,6 +267,19 @@ defmodule BlueBird.Test.GeneratorTest do
assert length(post_route.requests) == 1
end

test "includes request title" do
:get
|> build_conn("/waldorf")
|> Router.call(@opts)
|> ConnLogger.save(title: "Waldorf")

Logger.disable(self())
route = Generator.run() |> find_route("GET", "/waldorf")
request = hd(route.requests)

assert request.title == "Waldorf"
end

test "includes response status, headers and body" do
:get
|> build_conn("/waldorf")
Expand Down
3 changes: 2 additions & 1 deletion test/writer/blueprint_test.exs
Expand Up @@ -228,6 +228,7 @@ defmodule BlueBird.Test.Writer.BlueprintTest do
%Request{
method: POST,
path: "/users/:id/pets",
title: "Add pet",
headers: [{"accept", "application/json"}],
path_params: %{},
body_params: %{},
Expand Down Expand Up @@ -265,7 +266,7 @@ defmodule BlueBird.Test.Writer.BlueprintTest do
{"name":"George","kind":"dog"}
+ Request 200
+ Request Add pet
+ Headers
Expand Down

0 comments on commit 5ad3b48

Please sign in to comment.