Source

riak jiak.py client_id / apps / webmachine / src / webmachine_util.erl

Diff from to

apps/webmachine/src/webmachine_util.erl

 -export([unquote_header/1]).
 -export([now_diff_milliseconds/2]).
 -export([media_type_to_detail/1]).
--export([test/0]).
+
+-include_lib("eunit/include/eunit.hrl").
 
 convert_request_date(Date) ->
     try 
 	    "application/x-gzip";
         ".htc" ->
             "text/x-component";
+	".manifest" ->
+	    "text/cache-manifest";
+        ".svg" ->
+            "image/svg+xml";
 	_ ->
 	    "text/plain"
     end.
 		    QVal = case Val of
 			"1" ->
 			    1;
+                        [$.|_] ->
+                            %% handle strange FeedBurner Accept
+                            list_to_float([$0|Val]); 
 			_ -> list_to_float(Val)
 		    end,
 		    {QVal, Type, Rest ++ Acc};
             case PrioStr of
                 "0" -> {0.0, Choice};
                 "1" -> {1.0, Choice};
+                [$.|_] ->
+                    %% handle strange FeedBurner Accept
+                    {list_to_float([$0|PrioStr]), Choice};
                 _ -> {list_to_float(PrioStr), Choice}
             end;
         {Choice} ->
 now_diff_milliseconds({M,S,U}, {M1,S1,U1}) ->
     ((M-M1)*1000000+(S-S1))*1000 + ((U-U1) div 1000).
 
-test() ->
-    test_choose_media_type(),
-    ok.
+%%
+%% TEST
+%%
 
-test_choose_media_type() ->
+choose_media_type_test() ->
     Provided = "text/html",
     ShouldMatch = ["*", "*/*", "text/*", "text/html"],
     WantNone = ["foo", "text/xml", "application/*", "foo/bar/baz"],
-    [ Provided = choose_media_type([Provided], I) || I <- ShouldMatch ],
-    [ none = choose_media_type([Provided], I) || I <- WantNone ],
-    ok.
+    [ ?assertEqual(Provided, choose_media_type([Provided], I))
+      || I <- ShouldMatch ],
+    [ ?assertEqual(none, choose_media_type([Provided], I))
+      || I <- WantNone ].
+
+choose_media_type_qval_test() ->
+    Provided = ["text/html", "image/jpeg"],
+    HtmlMatch = ["image/jpeg;q=0.5, text/html",
+                 "text/html, image/jpeg; q=0.5",
+                 "text/*; q=0.8, image/*;q=0.7",
+                 "text/*;q=.8, image/*;q=.7"], %% strange FeedBurner format
+    JpgMatch = ["image/*;q=1, text/html;q=0.9",
+                "image/png, image/*;q=0.3"],
+    [ ?assertEqual("text/html", choose_media_type(Provided, I))
+      || I <- HtmlMatch ],
+    [ ?assertEqual("image/jpeg", choose_media_type(Provided, I))
+      || I <- JpgMatch ].
+
+convert_request_date_test() ->
+    ?assertMatch({{_,_,_},{_,_,_}},
+                 convert_request_date("Wed, 30 Dec 2009 14:39:02 GMT")),
+    ?assertMatch(bad_date,
+                 convert_request_date(<<"does not handle binaries">>)).
+
+compare_ims_dates_test() ->
+    Late = {{2009,12,30},{14,39,02}},
+    Early = {{2009,12,30},{13,39,02}},
+    ?assertEqual(true, compare_ims_dates(Late, Early)),
+    ?assertEqual(false, compare_ims_dates(Early, Late)).
+
+guess_mime_test() ->
+    TextTypes = [".html",".css",".htc",".manifest",".txt"],
+    AppTypes = [".xhtml",".xml",".js",".swf",".zip",".bz2",
+                ".gz",".tar",".tgz"],
+    ImgTypes = [".jpg",".jpeg",".gif",".png",".ico",".svg"],
+    ?assertEqual([], [ T || T <- TextTypes,
+                            1 /= string:str(guess_mime(T),"text/") ]),
+    ?assertEqual([], [ T || T <- AppTypes,
+                            1 /= string:str(guess_mime(T),"application/") ]),
+    ?assertEqual([], [ T || T <- ImgTypes,
+                            1 /= string:str(guess_mime(T),"image/") ]).
+
+unquote_header_test() ->
+    ?assertEqual("hello", unquote_header("hello")),
+    ?assertEqual("hello", unquote_header("\"hello\"")),
+    ?assertEqual("hello", unquote_header("\"hello")),
+    ?assertEqual("hello", unquote_header("\"\\h\\e\\l\\l\\o\"")).
+
+now_diff_milliseconds_test() ->
+    Late = {10, 10, 10},
+    Early1 = {10, 9, 9},
+    Early2 = {9, 9, 9},
+    ?assertEqual(1000, now_diff_milliseconds(Late, Early1)),
+    ?assertEqual(1000001000, now_diff_milliseconds(Late, Early2)).