Browse Source

Switch to using eunit for the unit tests.

Tim Fletcher 17 years ago
parent
commit
1427f4722a
6 changed files with 125 additions and 134 deletions
  1. 4 4
      Makefile
  2. 2 2
      README.txt
  3. 15 0
      include/oauth_test_macros.hrl
  4. 26 0
      src/oauth_termie.erl
  5. 0 128
      src/oauth_test.erl
  6. 78 0
      src/oauth_unit.erl

+ 4 - 4
Makefile

@@ -1,6 +1,6 @@
 SHELL=/bin/sh
 
-EFLAGS=-pa ebin -pa ../erlang-fmt/ebin
+EFLAGS=-pa ebin -pa ../erlang-fmt/ebin -pa ../eunit/ebin
 
 all: compile
 
@@ -12,10 +12,10 @@ clean:
 	rm -rf ebin erl_crash.dump
 
 test: compile
-	erl $(EFLAGS) -noshell -eval 'crypto:start(), oauth_test:all(), c:q().'
+	erl $(EFLAGS) -noshell -s crypto -s oauth_unit test -s init stop
 
 termie: compile
-	erl $(EFLAGS) -noshell -eval 'crypto:start(), inets:start(), oauth_test:termie(), c:q().'
+	erl $(EFLAGS) -noshell -s crypto -s inets -s oauth_termie test -s init stop
 
 i: compile
-	erl $(EFLAGS) -eval 'crypto:start(), inets:start().'
+	erl $(EFLAGS) -s crypto -s inets

+ 2 - 2
README.txt

@@ -51,8 +51,8 @@ would be similar to the following:
 
 
 Calling oauth:get or oauth:post returns an HTTP response tuple, as returned
-from http:request/4. Type "make termie", or look at oauth_test:termie/0 for
-a working example. Thanks Andy!
+from http:request/4. Type "make termie", or look at the oauth_termie module
+for a working example. Thanks Andy!
 
 Alternatively, you can use oauth_request:header/6 to generate an HTTP
 Authorization header, as described by http://oauth.net/core/1.0/#auth_header.

+ 15 - 0
include/oauth_test_macros.hrl

@@ -0,0 +1,15 @@
+-define(plaintext_signature_test(ConsumerSecret, TokenSecret, ExpectedSignature),
+  ?_assertEqual(ExpectedSignature, oauth_request:plaintext_signature(ConsumerSecret, TokenSecret))
+).
+
+-define(hmac_sha1_normalize_test(ExpectedString, Params),
+  ?_assertEqual(ExpectedString, oauth_request:hmac_sha1_normalize(Params))
+).
+
+-define(hmac_sha1_base_string_test(Method, URL, Params, Expected), fun() ->
+  ?assertEqual(string:join(Expected, ""), oauth_request:hmac_sha1_base_string(Method, URL, Params))
+end).
+
+-define(hmac_sha1_signature_test(ExpectedSignature, ConsumerSecret, TokenSecret, BaseString), fun() ->
+  ?assertEqual(ExpectedSignature, oauth_request:hmac_sha1_signature(string:join(BaseString, []), ConsumerSecret, TokenSecret))
+end).

+ 26 - 0
src/oauth_termie.erl

@@ -0,0 +1,26 @@
+-module(oauth_termie).
+
+-compile(export_all).
+
+% cf. http://term.ie/oauth/example/
+
+
+test() ->
+  test(oauth_consumer:new("key", "secret", "HMAC-SHA1")).
+
+test(Consumer) ->
+  RequestTokenURL = "http://term.ie/oauth/example/request_token.php",
+  test(Consumer, tee(oauth:tokens(oauth:get(RequestTokenURL, Consumer)))).
+
+test(Consumer, {ok, RequestTokenPair}) ->
+  AccessTokenURL = "http://term.ie/oauth/example/access_token.php",
+  AccessTokenResponse = tee(oauth:tokens(oauth:get(AccessTokenURL, Consumer, RequestTokenPair))),
+  test(Consumer, AccessTokenResponse, [{bar, "baz"}, {method, "foo"}]).
+
+test(Consumer, {ok, AccessTokenPair}, EchoParams) ->
+  EchoURL = "http://term.ie/oauth/example/echo_api.php",
+  {ok, {_,_,Data}} = tee(oauth:get(EchoURL, Consumer, AccessTokenPair, EchoParams)),
+  tee(lists:keysort(1, oauth:params_from_string(Data))).
+
+tee(X) ->
+  error_logger:info_msg("~p~n~n", [X]), X.

+ 0 - 128
src/oauth_test.erl

@@ -1,128 +0,0 @@
--module(oauth_test).
-
--compile(export_all).
-
-
-all() ->
-  lists:all(fun(F) ->
-    io:format("~s:~s~n", [?MODULE, F]),
-    apply(?MODULE, F, [])
-  end, [
-    params_from_string,
-    params_to_header_string,
-    plaintext_signature,
-    hmac_sha1_normalize,
-    hmac_sha1_base_string,
-    hmac_sha1_signature
-  ]),
-  ok.
-
-params_from_string() ->
-  % cf. http://oauth.net/core/1.0/#response_parameters (5.3)
-  should_be_equal([{oauth_token, "ab3cd9j4ks73hf7g"}, {oauth_token_secret, "xyz4992k83j47x0b"}],
-  oauth:params_from_string("oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=xyz4992k83j47x0b")).
-
-params_to_header_string() ->
-  % cf. http://oauth.net/core/1.0/#auth_header_authorization (5.4.1)
-  "oauth_consumer_key=\"0685bd9184jfhq22\",oauth_token=\"ad180jjd733klru7\"" =:=
-  oauth_request:params_to_header_string([{oauth_consumer_key, "0685bd9184jfhq22"}, {oauth_token, "ad180jjd733klru7"}]).
-
-plaintext_signature() ->
-  % cf. http://oauth.net/core/1.0/#rfc.section.9.4.1
-  ConsumerSecret="djr9rjt0jd78jf88",
-  lists:all(fun({TokenSecret, Expected}) ->
-    Actual = oauth_request:plaintext_signature(ConsumerSecret, TokenSecret),
-    should_be_equal(Expected, Actual)
-  end, [
-    {"jjd999tj88uiths3","djr9rjt0jd78jf88%26jjd999tj88uiths3"},
-    {"jjd99$tj88uiths3","djr9rjt0jd78jf88%26jjd99%2524tj88uiths3"},
-    {"", "djr9rjt0jd78jf88%26"}
-  ]).
-
-hmac_sha1_normalize() ->
-  % cf. http://wiki.oauth.net/TestCases
-  lists:all(fun({Params, Expected}) ->
-    should_be_equal(Expected, oauth_request:hmac_sha1_normalize(Params))
-  end, [
-    {[{name,undefined}], "name="},
-    {[{a,b}], "a=b"},
-    {[{a,b},{c,d}], "a=b&c=d"},
-    {[{a,"x!y"},{a,"x y"}], "a=x%20y&a=x%21y"},
-    {[{"x!y",a},{x,a}], "x=a&x%21y=a"}
-  ]).
-
-hmac_sha1_base_string() ->
-  % cf. http://wiki.oauth.net/TestCases
-  lists:all(fun({MethodString, URL, Params, Expected}) ->
-    Actual = oauth_request:hmac_sha1_base_string(MethodString, URL, Params),
-    should_be_equal(Expected, Actual)
-  end, [
-    {"GET", "http://example.com", [
-      {n,v}
-    ],
-      "GET&http%3A%2F%2Fexample.com&n%3Dv"
-    },
-    {"POST", "https://photos.example.net/request_token", [
-      {oauth_version, "1.0"},
-      {oauth_consumer_key, "dpf43f3p2l4k3l03"},
-      {oauth_timestamp, "1191242090"},
-      {oauth_nonce, "hsu94j3884jdopsl"},
-      {oauth_signature_method, "PLAINTEXT"}
-    ],
-      "POST&https%3A%2F%2Fphotos.example.net%2Frequest_token&oauth_consumer_key" ++
-      "%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884jdopsl%26oauth_signature_method" ++
-      "%3DPLAINTEXT%26oauth_timestamp%3D1191242090%26oauth_version%3D1.0"
-    },
-    {"GET", "http://photos.example.net/photos", [
-      {file, "vacation.jpg"},
-      {size, "original"},
-      {oauth_version, "1.0"},
-      {oauth_consumer_key, "dpf43f3p2l4k3l03"},
-      {oauth_token, "nnch734d00sl2jdk"},
-      {oauth_timestamp, "1191242096"},
-      {oauth_nonce, "kllo9940pd9333jh"},
-      {oauth_signature_method, "HMAC-SHA1"}
-    ],
-      "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" ++
-      "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26" ++
-      "oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26" ++
-      "oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal"
-    }
-  ]).
-
-hmac_sha1_signature() ->
-  % cf. http://wiki.oauth.net/TestCases
-  lists:all(fun({Expected, ConsumerSecret, TokenSecret, BaseString}) ->
-    Actual = oauth_request:hmac_sha1_signature(BaseString, ConsumerSecret, TokenSecret),
-    should_be_equal(Expected, Actual)
-  end, [
-    {"egQqG5AJep5sJ7anhXju1unge2I=", "cs", "", "bs"},
-    {"VZVjXceV7JgPq/dOTnNmEfO0Fv8=", "cs", "ts", "bs"},
-    {"tR3+Ty81lMeYAr/Fid0kMTYa/WM=", "kd94hf93k423kf44", "pfkkdhi9sl3r4s00",
-      "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26" ++
-      "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26" ++
-      "oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26" ++
-      "oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal"
-    }
-  ]).
-
-termie() ->
-  % cf. http://term.ie/oauth/example/
-  Consumer = oauth_consumer:new("key", "secret", "HMAC-SHA1"),
-  RequestTokenURL = "http://term.ie/oauth/example/request_token.php",
-  AccessTokenURL = "http://term.ie/oauth/example/access_token.php",
-  EchoURL = "http://term.ie/oauth/example/echo_api.php",
-  EchoParams = [{bar, "baz"}, {method, "foo"}],
-  {ok, Tokens} = tee(oauth:tokens(oauth:get(RequestTokenURL, Consumer))),  
-  {ok, AccessTokens} = tee(oauth:tokens(oauth:get(AccessTokenURL, Consumer, Tokens))),
-  {ok, {_,_,Data}} = tee(oauth:get(EchoURL, Consumer, AccessTokens, EchoParams)),
-  should_be_equal(lists:keysort(1, EchoParams), lists:keysort(1, oauth:params_from_string(Data))).
-
-tee(X) ->
-  io:format("~p~n", [X]), X.
-
-should_be_equal(X, X) ->
-  true;
-should_be_equal(X, Y) ->
-  io:format("~p (expected) is not equal to ~p~n", [X, Y]),
-  false.

+ 78 - 0
src/oauth_unit.erl

@@ -0,0 +1,78 @@
+-module(oauth_unit).
+
+-include_lib("eunit/include/eunit.hrl").
+-include("oauth_test_macros.hrl").
+
+
+params_from_string_test_() ->
+  % cf. http://oauth.net/core/1.0/#response_parameters (5.3)
+  Params = oauth:params_from_string("oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=xyz4992k83j47x0b"), [
+  ?_assertEqual("ab3cd9j4ks73hf7g", proplists:get_value(oauth_token, Params)),
+  ?_assertEqual("xyz4992k83j47x0b", proplists:get_value(oauth_token_secret, Params))
+].
+
+params_to_header_string_test_() ->
+  % cf. http://oauth.net/core/1.0/#auth_header_authorization (5.4.1)
+  Params = [{oauth_consumer_key, "0685bd9184jfhq22"}, {oauth_token, "ad180jjd733klru7"}],
+  String = "oauth_consumer_key=\"0685bd9184jfhq22\",oauth_token=\"ad180jjd733klru7\"", [
+  ?_assertEqual(String, oauth_request:params_to_header_string(Params))
+].
+
+plaintext_signature_test_() -> [
+  % cf. http://oauth.net/core/1.0/#rfc.section.9.4.1
+  ?plaintext_signature_test("djr9rjt0jd78jf88", "jjd999tj88uiths3", "djr9rjt0jd78jf88%26jjd999tj88uiths3"),
+  ?plaintext_signature_test("djr9rjt0jd78jf88", "jjd99$tj88uiths3", "djr9rjt0jd78jf88%26jjd99%2524tj88uiths3"),
+  ?plaintext_signature_test("djr9rjt0jd78jf88", "", "djr9rjt0jd78jf88%26")
+].
+
+hmac_sha1_normalize_test_() -> [
+  % cf. http://wiki.oauth.net/TestCases
+  ?hmac_sha1_normalize_test("name=", [{name,undefined}]),
+  ?hmac_sha1_normalize_test("a=b", [{a,b}]),
+  ?hmac_sha1_normalize_test("a=b&c=d", [{a,b},{c,d}]),
+  ?hmac_sha1_normalize_test("a=x%20y&a=x%21y", [{a,"x!y"},{a,"x y"}]),
+  ?hmac_sha1_normalize_test("x=a&x%21y=a", [{"x!y",a},{x,a}])
+].
+
+hmac_sha1_base_string_test_() -> [
+  % cf. http://wiki.oauth.net/TestCases
+  ?hmac_sha1_base_string_test("GET", "http://example.com", [{n,v}], ["GET&http%3A%2F%2Fexample.com&n%3Dv"]),
+  ?hmac_sha1_base_string_test("POST", "https://photos.example.net/request_token", [
+    {oauth_version, "1.0"},
+    {oauth_consumer_key, "dpf43f3p2l4k3l03"},
+    {oauth_timestamp, "1191242090"},
+    {oauth_nonce, "hsu94j3884jdopsl"},
+    {oauth_signature_method, "PLAINTEXT"}
+  ], [
+    "POST&https%3A%2F%2Fphotos.example.net%2Frequest_token&oauth_consumer_key",
+    "%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dhsu94j3884jdopsl%26oauth_signature_method",
+    "%3DPLAINTEXT%26oauth_timestamp%3D1191242090%26oauth_version%3D1.0"
+  ]),
+  ?hmac_sha1_base_string_test("GET", "http://photos.example.net/photos", [
+    {file, "vacation.jpg"},
+    {size, "original"},
+    {oauth_version, "1.0"},
+    {oauth_consumer_key, "dpf43f3p2l4k3l03"},
+    {oauth_token, "nnch734d00sl2jdk"},
+    {oauth_timestamp, "1191242096"},
+    {oauth_nonce, "kllo9940pd9333jh"},
+    {oauth_signature_method, "HMAC-SHA1"}
+  ], [
+    "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26",
+    "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26",
+    "oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26",
+    "oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal"
+  ])
+].
+
+hmac_sha1_signature_test_() -> [
+  % cf. http://wiki.oauth.net/TestCases
+  ?hmac_sha1_signature_test("egQqG5AJep5sJ7anhXju1unge2I=", "cs", "", ["bs"]),
+  ?hmac_sha1_signature_test("VZVjXceV7JgPq/dOTnNmEfO0Fv8=", "cs", "ts", ["bs"]),
+  ?hmac_sha1_signature_test("tR3+Ty81lMeYAr/Fid0kMTYa/WM=", "kd94hf93k423kf44", "pfkkdhi9sl3r4s00", [
+    "GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26",
+    "oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26",
+    "oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26",
+    "oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal"
+  ])
+].