aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-12-05 22:53:59 +0100
committerLoïc Hoguin <[email protected]>2011-12-05 23:05:32 +0100
commitaab1587a4b3d8f0c3d92a2083227527d51109980 (patch)
tree4ac3dc54b3736a4ea86aff716fc7ce6812bf9188 /test
parent7acaa996ed8dea5723df6e83729236e57e6eb850 (diff)
downloadcowboy-aab1587a4b3d8f0c3d92a2083227527d51109980.tar.gz
cowboy-aab1587a4b3d8f0c3d92a2083227527d51109980.tar.bz2
cowboy-aab1587a4b3d8f0c3d92a2083227527d51109980.zip
Add experimental Webmachine based REST protocol support
As with everything experimental, it probably has a lot of bugs and may not even work. Like Websocket, REST must be upgraded from a standard resource through the init/3 function. A key difference between Webmachine and Cowboy's REST protocol handler is that in Cowboy the resource has direct access to the request object. This makes a small change in a few places where you were expected to return headers or body in Webmachine and are now expected to set them directly yourself if needed (options/2, for example). Another difference is that the functions rest_init/2 will always be called when starting to process a request. Similarly, rest_terminate/2 will be called when the process completes successfully. The Cowboy REST support also includes automatic language selection, thanks to the languages_provided/2 callback. Finally, Cowboy REST expects full URIs to be given at all times, and will not try to reconstruct URIs from fragments. Note that REST requests cannot be chained (keepalive) at this time. This is a design issue in cowboy_http_protocol that will be fixed soon. Check out the source for more details. It has been designed to be very easy to read and understand so if you don't understand something, it's probably a bug. Thanks in advance for all the great bug reports, pull requests and comments you'll forward my way!
Diffstat (limited to 'test')
-rw-r--r--test/http_SUITE.erl21
-rw-r--r--test/rest_simple_resource.erl12
2 files changed, 31 insertions, 2 deletions
diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl
index 5f02a7c..eb0bf54 100644
--- a/test/http_SUITE.erl
+++ b/test/http_SUITE.erl
@@ -25,11 +25,12 @@
set_resp_overwrite/1, set_resp_body/1]). %% http.
-export([http_200/1, http_404/1]). %% http and https.
-export([http_10_hostless/1]). %% misc.
+-export([rest_simple/1]). %% rest.
%% ct.
all() ->
- [{group, http}, {group, https}, {group, misc}].
+ [{group, http}, {group, https}, {group, misc}, {group, rest}].
groups() ->
BaseTests = [http_200, http_404],
@@ -38,7 +39,9 @@ groups() ->
ws0, ws8, ws8_single_bytes, ws8_init_shutdown, ws13,
ws_timeout_hibernate, set_resp_header,
set_resp_overwrite, set_resp_body] ++ BaseTests},
- {https, [], BaseTests}, {misc, [], [http_10_hostless]}].
+ {https, [], BaseTests},
+ {misc, [], [http_10_hostless]},
+ {rest, [], [rest_simple]}].
init_per_suite(Config) ->
application:start(inets),
@@ -77,6 +80,14 @@ init_per_group(misc, Config) ->
cowboy_http_protocol, [{dispatch, [{'_', [
{[], http_handler, []}
]}]}]),
+ [{port, Port}|Config];
+init_per_group(rest, Config) ->
+ Port = 33083,
+ cowboy:start_listener(reset, 100,
+ cowboy_tcp_transport, [{port, Port}],
+ cowboy_http_protocol, [{dispatch, [{'_', [
+ {[<<"simple">>], rest_simple_resource, []}
+ ]}]}]),
[{port, Port}|Config].
end_per_group(https, _Config) ->
@@ -535,3 +546,9 @@ http_404(Config) ->
http_10_hostless(Config) ->
Packet = "GET / HTTP/1.0\r\n\r\n",
{Packet, 200} = raw_req(Packet, Config).
+
+%% rest.
+
+rest_simple(Config) ->
+ Packet = "GET /simple HTTP/1.1\r\nHost: localhost\r\n\r\n",
+ {Packet, 200} = raw_req(Packet, Config).
diff --git a/test/rest_simple_resource.erl b/test/rest_simple_resource.erl
new file mode 100644
index 0000000..e2c573c
--- /dev/null
+++ b/test/rest_simple_resource.erl
@@ -0,0 +1,12 @@
+-module(rest_simple_resource).
+-export([init/3, content_types_provided/2, get_text_plain/2]).
+
+init(_Transport, _Req, _Opts) ->
+ {upgrade, protocol, cowboy_http_rest}.
+
+content_types_provided(Req, State) ->
+ {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
+
+get_text_plain(Req, State) ->
+ {<<"This is REST!">>, Req, State}.
+