1-module(headers_SUITE). 2 3-include("testsuite.hrl"). 4 5-compile(export_all). 6 7all() -> 8 [ 9 set_headers, 10 get_headers, 11 delete_headers, 12 merge_headers 13 ]. 14 15groups() -> 16 [ 17 ]. 18 19%%==================================================================== 20init_per_suite(Config) -> 21 Config. 22 23end_per_suite(_Config) -> 24 ok. 25 26init_per_group(_Group, Config) -> 27 Config. 28 29end_per_group(_Group, _Config) -> 30 ok. 31 32init_per_testcase(_Test, Config) -> 33 Config. 34 35end_per_testcase(_Test, _Config) -> 36 ok. 37 38%%==================================================================== 39set_headers(_Config) -> 40 Value = "test value", 41 lists:foreach( 42 fun({Hdr, StrHdr, Fun}) -> 43 NewHdrs = yaws_api:set_header(#headers{}, Hdr, Value), 44 ?assertEqual(Value, yaws_api:Fun(NewHdrs)), 45 ?assertEqual(Value, yaws_api:get_header(NewHdrs, StrHdr)), 46 ?assertEqual(0, length(NewHdrs#headers.other)) 47 end, 48 field_headers() 49 ), 50 Hdrs = yaws_api:set_header(create_headers(99), age, "123"), 51 ?assertEqual(100, length(Hdrs#headers.other)), 52 ok. 53 54get_headers(_Config) -> 55 ?assertEqual(undefined, yaws_api:get_header(#headers{}, accept)), 56 ?assertEqual(undefined, yaws_api:get_header(#headers{}, "Connection")), 57 ?assertEqual(none, yaws_api:get_header(#headers{}, range, none)), 58 ?assertEqual(none, yaws_api:get_header(#headers{}, "No-Such-Header", none)), 59 Hdrs = create_headers(100), 60 lists:foreach( 61 fun(I) -> 62 L = integer_to_list(I), 63 Val = "value"++L, 64 Hdr = "X-HEADER-"++L, 65 ?assertEqual(Val, yaws_api:get_header(Hdrs, Hdr)) 66 end, 67 lists:seq(1,100) 68 ), 69 ok. 70 71delete_headers(_Config) -> 72 Headers = create_headers(10), 73 Res = lists:foldl( 74 fun(I, {Size, Hdrs}) -> 75 ?assertEqual(Size, length(Hdrs#headers.other)), 76 L = integer_to_list(I), 77 Hdr = "X-Header-"++L, 78 NHdrs = yaws_api:delete_header(Hdrs, Hdr), 79 NewSize = Size - 1, 80 ?assertEqual(NewSize, length(NHdrs#headers.other)), 81 {NewSize, NHdrs} 82 end, 83 {10, Headers}, 84 lists:seq(1,10) 85 ), 86 ?assertEqual({0, #headers{}}, Res), 87 ok. 88 89merge_headers(_Config) -> 90 Hdrs0 = create_headers(10), 91 Hdrs1 = yaws_api:merge_header(Hdrs0, <<"x-header-7">>, <<"another-value">>), 92 Val1 = yaws_api:get_header(Hdrs1, 'x-header-7'), 93 Expected1 = lists:sort(["value7", "another-value"]), 94 ?assertEqual(Expected1, lists:sort(string:tokens(Val1, ", "))), 95 Hdrs2 = yaws_api:set_header(Hdrs1, "set-cookie", "user=joe"), 96 Hdrs3 = yaws_api:merge_header(Hdrs2, "set-cookie", "domain=erlang.org"), 97 Val2 = yaws_api:get_header(Hdrs3, "set-cookie"), 98 Expected2 = {multi, ["user=joe", "domain=erlang.org"]}, 99 ?assertEqual(Expected2, Val2), 100 ok. 101 102create_headers(N) -> 103 lists:foldl(fun({Hdr,Val}, Hdrs) -> 104 yaws_api:set_header(Hdrs, Hdr, Val) 105 end, 106 #headers{}, 107 [begin 108 L = integer_to_list(I), 109 {"X-Header-"++L, "value"++L} 110 end || I <- lists:seq(1,N)]). 111 112%% these headers are the fields in the #headers{} record 113field_headers() -> 114 [{connection, "Connection", headers_connection}, 115 {accept, "Accept", headers_accept}, 116 {host, "Host", headers_host}, 117 {if_modified_since,"If-Modified-Since", headers_if_modified_since}, 118 {if_match, "If-Match", headers_if_match}, 119 {if_none_match, "If-None-Match", headers_if_none_match}, 120 {if_range, "if-Range", headers_if_range}, 121 {if_unmodified_since, "If-Unmodified-Since", headers_if_unmodified_since}, 122 {range, "Range", headers_range}, 123 {referer, "Referer", headers_referer}, 124 {user_agent, "User-Agent", headers_user_agent}, 125 {accept_ranges, "Accept-Ranges", headers_accept_ranges}, 126 {cookie, "Cookie", headers_cookie}, 127 {keep_alive, "Keep-Alive", headers_keep_alive}, 128 {location, "Location", headers_location}, 129 {content_length, "Content-Length", headers_content_length}, 130 {content_type, "Content-Type", headers_content_type}, 131 {content_encoding, "Content-Encoding", headers_content_encoding}, 132 {authorization, "Authorization", headers_authorization}, 133 {transfer_encoding, "Transfer-Encoding", headers_transfer_encoding}, 134 {x_forwarded_for, "X-Forwarded-For", headers_x_forwarded_for}]. 135