-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cabol.3.cowboy swagger handler #10
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
trails | ||
]}, | ||
{modules, []}, | ||
{mod, {cowboy_swagger, []}}, | ||
{mod, {cowboy_swagger_app, []}}, | ||
{registered, []} | ||
] | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
%%% @hidden | ||
-module(cowboy_swagger_app). | ||
|
||
-behaviour(application). | ||
|
||
%% Application callbacks | ||
-export([start/2, stop/1]). | ||
|
||
-spec start(term(), term()) -> {error, term()} | {ok, pid()}. | ||
start(_Type, _Args) -> | ||
cowboy_swagger_sup:start_link(). | ||
|
||
-spec stop(term()) -> ok. | ||
stop(_State) -> | ||
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
-module(cowboy_swagger_handler). | ||
|
||
%% Cowboy callbacks | ||
-export([ init/3 | ||
, rest_init/2 | ||
, content_types_provided/2 | ||
]). | ||
|
||
%% Handlers | ||
-export([handle_get/2]). | ||
|
||
%% Trails | ||
-behaviour(trails_handler). | ||
-export([trails/0]). | ||
|
||
-type state() :: #{}. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Cowboy Callbacks | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec init({atom(), atom()}, cowboy_req:req(), state()) -> | ||
{upgrade, protocol, cowboy_rest}. | ||
init(_Transport, _Req, _Opts) -> | ||
{upgrade, protocol, cowboy_rest}. | ||
|
||
-spec rest_init(cowboy_req:req(), state()) -> | ||
{ok, cowboy_req:req(), term()}. | ||
rest_init(Req, _Opts) -> | ||
{ok, Req, #{}}. | ||
|
||
-spec content_types_provided(cowboy_req:req(), state()) -> | ||
{[term()], cowboy_req:req(), state()}. | ||
content_types_provided(Req, State) -> | ||
{[{<<"application/json">>, handle_get}], Req, State}. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Handlers | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%% @hidden | ||
handle_get(Req, State) -> | ||
Trails = trails:all(), | ||
{cowboy_swagger:to_json(Trails), Req, State}. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Trails | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%% @hidden | ||
trails() -> | ||
MD = | ||
#{get => | ||
#{description => "Retrives swagger's specification.", | ||
produces => ["application/json"] | ||
} | ||
}, | ||
[trails:trail("/api-docs/swagger.json", cowboy_swagger_handler, [], MD)]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
%%% @hidden | ||
-module(cowboy_swagger_sup). | ||
|
||
-behaviour(supervisor). | ||
|
||
-export([init/1]). | ||
-export([start_link/0]). | ||
|
||
-spec start_link() -> {ok, pid()} | {error, term()}. | ||
start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
-spec init([]) -> {ok, {{one_for_one, 10, 60}, []}}. | ||
init([]) -> | ||
{ok, {{one_for_one, 10, 60}, []}}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
%% Specific modules to include in cover. | ||
{ | ||
incl_mods, | ||
[cowboy_swagger] | ||
[ | ||
cowboy_swagger, | ||
cowboy_swagger_handler | ||
] | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-module(cowboy_swagger_handler_SUITE). | ||
|
||
%% CT | ||
-export([ all/0 | ||
, init_per_suite/1 | ||
, end_per_suite/1 | ||
]). | ||
|
||
%% Test cases | ||
-export([handler_test/1]). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Common test | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec all() -> [atom()]. | ||
all() -> | ||
cowboy_swagger_test_utils:all(?MODULE). | ||
|
||
-spec init_per_suite( | ||
cowboy_swagger_test_utils:config() | ||
) -> cowboy_swagger_test_utils:config(). | ||
init_per_suite(Config) -> | ||
shotgun:start(), | ||
example:start(), | ||
Config. | ||
|
||
-spec end_per_suite( | ||
cowboy_swagger_test_utils:config() | ||
) -> cowboy_swagger_test_utils:config(). | ||
end_per_suite(Config) -> | ||
shotgun:stop(), | ||
example:stop(), | ||
Config. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%% Test Cases | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec handler_test(cowboy_swagger_test_utils:config()) -> {atom(), string()}. | ||
handler_test(_Config) -> | ||
%% Expected result | ||
Trails = trails:trails([example_echo_handler, | ||
example_description_handler, | ||
cowboy_swagger_handler]), | ||
ExpectedPaths = cowboy_swagger:dec_json( | ||
cowboy_swagger:enc_json(cowboy_swagger:swagger_paths(Trails))), | ||
|
||
%% GET swagger.json spec | ||
ct:comment("GET /api-docs/swagger.json should return 200 OK"), | ||
#{status_code := 200, body := Body0} = | ||
cowboy_swagger_test_utils:api_call(get, "/api-docs/swagger.json"), | ||
#{<<"paths">> := ExpectedPaths} = cowboy_swagger:dec_json(Body0), | ||
|
||
{comment, ""}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
-module(cowboy_swagger_test_utils). | ||
|
||
-export([ all/1 | ||
, init_per_suite/1 | ||
, end_per_suite/1 | ||
]). | ||
-export([ api_call/2 | ||
, api_call/3 | ||
, api_call/4 | ||
]). | ||
|
||
-type config() :: proplists:proplist(). | ||
-export_type([config/0]). | ||
|
||
-spec all(atom()) -> [atom()]. | ||
all(Module) -> | ||
ExcludedFuns = [module_info, init_per_suite, end_per_suite, group, all], | ||
Exports = Module:module_info(exports), | ||
[F || {F, 1} <- Exports, not lists:member(F, ExcludedFuns)]. | ||
|
||
-spec init_per_suite(config()) -> config(). | ||
init_per_suite(Config) -> | ||
Config. | ||
|
||
-spec end_per_suite(config()) -> config(). | ||
end_per_suite(Config) -> | ||
Config. | ||
|
||
-spec api_call(atom(), string()) -> #{}. | ||
api_call(Method, Uri) -> | ||
api_call(Method, Uri, #{}). | ||
|
||
-spec api_call(atom(), string(), #{}) -> #{}. | ||
api_call(Method, Uri, Headers) -> | ||
api_call(Method, Uri, Headers, []). | ||
|
||
-spec api_call(atom(), string(), #{}, iodata()) -> #{}. | ||
api_call(Method, Uri, Headers, Body) -> | ||
Port = application:get_env(example, http_port, 8080), | ||
{ok, Pid} = shotgun:open("localhost", Port), | ||
try | ||
{ok, Response} = shotgun:request(Pid, Method, Uri, Headers, Body, #{}), | ||
Response | ||
after | ||
shotgun:close(Pid) | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{application, example, | ||
[ | ||
{description, "Cowboy Trails Basic Example."}, | ||
{vsn, "0.1"}, | ||
{applications, | ||
[kernel, | ||
stdlib, | ||
cowboy, | ||
trails, | ||
cowboy_swagger | ||
]}, | ||
{modules, []}, | ||
{mod, {example, []}}, | ||
{start_phases, [{start_trails_http, []}]} | ||
] | ||
}. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Elvis:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Elvis: