From 57901a71160e2592d999efb564bef4a50876d6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 16 Jun 2016 22:17:14 +0200 Subject: Fix upload example --- examples/upload/src/upload_app.erl | 8 ++++---- test/examples_SUITE.erl | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/examples/upload/src/upload_app.erl b/examples/upload/src/upload_app.erl index 5285c0f..468d15d 100644 --- a/examples/upload/src/upload_app.erl +++ b/examples/upload/src/upload_app.erl @@ -13,12 +13,12 @@ start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [ {"/", cowboy_static, {priv_file, upload, "index.html"}}, - {"/upload", upload_handler, []}, - {"/files/[...]", cowboy_static, {priv_dir, upload, "files"}} + {"/upload", upload_handler, []} ]} ]), - {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], - [{env, [{dispatch, Dispatch}]}]), + {ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{ + env => #{dispatch => Dispatch} + }), upload_sup:start_link(). stop(_State) -> diff --git a/test/examples_SUITE.erl b/test/examples_SUITE.erl index 7092701..5263a6a 100644 --- a/test/examples_SUITE.erl +++ b/test/examples_SUITE.erl @@ -348,6 +348,34 @@ do_markdown_middleware(Transport, Protocol, Config) -> {_, <<"text/html">>} = lists:keyfind(<<"content-type">>, 1, Headers), ok. +%% Upload. + +upload(Config) -> + doc("Upload example."), + try + do_compile_and_start(upload), + do_upload(tcp, http, Config), + do_upload(tcp, http2, Config) + after + do_stop(upload) + end. + +do_upload(Transport, Protocol, Config) -> + {200, _, << "", _/bits >>} = do_get(Transport, Protocol, "/", Config), + %% Use POST to upload a file using multipart. + ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]), + Ref = gun:post(ConnPid, "/upload", [ + {<<"content-type">>, <<"multipart/form-data;boundary=deadbeef">>} + ], << + "--deadbeef\r\n" + "Content-Disposition: form-data; name=\"inputfile\"; filename=\"test.txt\"\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "Cowboy upload example!\r\n" + "--deadbeef--">>), + {response, fin, 204, _} = gun:await(ConnPid, Ref), + ok. + %% Websocket. websocket(_) -> -- cgit v1.2.3