Created
March 11, 2011 22:14
-
-
Save dreid/866672 to your computer and use it in GitHub Desktop.
Manually construct a parameterized module… the advantage here is multiple new functions and other static functions which do not take a module argument.
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
%% @author David Reid <[email protected]> | |
%% @copyright 2011 Mochi Media, Inc. | |
%% @doc Stub MochiWeb HTTP Request module implementation for testing. | |
-module(stub_mochiweb_request). | |
-record(stub_mochiweb_request, {options, headers, response_pid}). | |
%% Constructors and static method exports | |
-export([new/1, new/2, new/3]). | |
-export([response/0]). | |
%% Stub exports | |
-export([get/2]). | |
-export([parse_post/1]). | |
-export([get_header_value/2]). | |
-export([ok/2]). | |
%% Constructors and static method implementations | |
%% @spec new(list()) -> stub_mochiweb_request | |
%% | |
%% | |
new(Options) -> | |
new(Options, []). | |
new(Options, Headers) -> | |
new(Options, Headers, self()). | |
new(Options, Headers, ResponsePid) -> | |
#stub_mochiweb_request{options=Options, | |
headers=Headers, | |
response_pid=ResponsePid}. | |
response() -> | |
receive | |
{mochiweb_response, Response} -> | |
Response | |
end. | |
%% Stub implementation | |
%% @spec get(field()) -> term() | |
%% @doc Return the internal representation of the given field. | |
%% Field must be present in the Options proplist when | |
%% this module was created by stub_mochiweb_request:new/1 | |
get(Option, S) -> | |
case lists:keyfind(Option, 1, S#stub_mochiweb_request.options) of | |
{Option, Value} -> | |
Value; | |
false -> | |
throw({unknown_option, Option}) | |
end. | |
parse_post(S) -> | |
?MODULE:get(post_variables, S). | |
get_header_value(Header, S) -> | |
case lists:keyfind(Header, 1, S#stub_mochiweb_request.headers) of | |
{Header, Value} -> | |
Value; | |
false -> | |
undefined | |
end. | |
ok(Response, S) -> | |
S#stub_mochiweb_request.response_pid ! | |
{mochiweb_response, {ok, Response}}, | |
ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment