📄 test_http.c
字号:
http_etag_init(http_etag); http_expect_init(http_expect); http_last_modified_init(http_last_modified); http_location_init(http_location); http_max_forwards_init(http_max_forwards); http_pragma_init(http_pragma); http_range_init(http_range); http_retry_after_init(http_retry_after); http_trailer_init(http_trailer); http_upgrade_init(http_upgrade); http_vary_init(http_vary); http_warning_init(http_warning); http_user_agent_init(http_user_agent); http_server_init(http_server); http_mime_version_init(http_mime_version); http_content_language_init(http_content_language); http_content_location_init(http_content_location); http_content_md5_init(http_content_md5); http_content_range_init(http_content_range); http_content_encoding_init(http_content_encoding); http_transfer_encoding_init(http_transfer_encoding); http_content_type_init(http_content_type); http_content_length_init(http_content_length); home = su_home_new(sizeof *home); { int i; struct { http_method_t number; char const *name; } methods[] = { { http_method_get, "GET" }, { http_method_post, "POST" }, { http_method_head, "HEAD" }, { http_method_options, "OPTIONS" }, { http_method_put, "PUT" }, { http_method_delete, "DELETE" }, { http_method_trace, "TRACE" }, { http_method_connect, "CONNECT" }, { 0, NULL } }; for (i = 0; methods[i].name; i++) { TEST_1(strcmp(methods[i].name, http_method_name(methods[i].number, "")) == 0); } } msg = read_message( "PUT /Foo HTTP/1.1\r\n" "Host: [::1]:8080\r\n" "From: webmaster@w3.org\r\n" "Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)\r\n" "Referer: http://www.w3.org/hypertext/DataSources/Overview.html\r\n" "Connection: close\r\n" "Accept: audio/*; q=0.2, audio/basic\r\n" "Accept-Charset: iso-8859-5, unicode-1-1;q=0.8\r\n" "Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0\r\n" "Accept-Language: da, en-gb;q=0.8, en;q=0.7\r\n" "Accept-Ranges: bytes\r\n" "Age: 212\r\n" "Allow: GET, HEAD, PUT\r\n" "Cache-Control: private, community=\"UCI\"\r\n" "Content-Encoding: identity\r\n" "Content-Language: da\r\n" "Content-Type: text/html\r\n" "Content-Location: http://localhost/Foo\r\n" "Content-Length: 28\r\n" "Content-MD5: f48BLMCjkX5M5PgWoelogA==\r\n" "Content-Range: bytes 0-27/*\r\n" "\r\n" "<html><body></body></html>\r\n"); http = msg_object(msg); TEST_1(home && msg && http); TEST_1(http_is_request((http_header_t *)http->http_request)); TEST_1(http->http_via); TEST_1(http->http_via->v_next); TEST_1(http->http_via->v_next->v_next == NULL); TEST_1(http->http_host); TEST_1(http->http_from); TEST_1(http->http_referer); TEST_1(http->http_connection); TEST_1(http->http_accept); TEST_1(http->http_accept_charset); TEST_1(http->http_accept_encoding); TEST_1(http->http_accept_language); TEST_1(http->http_accept_ranges); TEST_1(http->http_age); TEST_1(http->http_allow); TEST_1(http->http_cache_control); TEST_1(http->http_content_encoding); TEST_1(http->http_content_language); TEST_1(http->http_content_type); TEST_1(http->http_content_location); TEST_1(http->http_content_length); TEST_1(http->http_content_md5); TEST_1(http->http_content_range); /* Quiet lots of warnings */#define _msg_header_offset msg_header_offset#define msg_header_offset(msg, http, h) \ _msg_header_offset(msg, http, (http_header_t *)h) TEST_P(msg_header_offset(msg, http, http_request), &http->http_request); TEST_P(msg_header_offset(msg, http, http_status), &http->http_status); TEST_P(msg_header_offset(msg, http, http_unknown), &http->http_unknown); TEST_P(msg_header_offset(msg, http, http_separator), &http->http_separator); TEST_P(msg_header_offset(msg, http, http_payload), &http->http_payload); TEST_P(msg_header_offset(msg, http, http_via), &http->http_via); TEST_P(msg_header_offset(msg, http, http_via), &http->http_via); TEST_P(msg_header_offset(msg, http, http_host), &http->http_host); TEST_P(msg_header_offset(msg, http, http_from), &http->http_from); TEST_P(msg_header_offset(msg, http, http_referer), &http->http_referer); TEST_P(msg_header_offset(msg, http, http_connection), &http->http_connection); TEST_P(msg_header_offset(msg, http, http_accept), &http->http_accept); TEST_P(msg_header_offset(msg, http, http_accept_charset), &http->http_accept_charset); TEST_P(msg_header_offset(msg, http, http_accept_encoding), &http->http_accept_encoding); TEST_P(msg_header_offset(msg, http, http_accept_language), &http->http_accept_language); TEST_P(msg_header_offset(msg, http, http_accept_ranges), &http->http_accept_ranges); TEST_P(msg_header_offset(msg, http, http_allow), &http->http_allow); TEST_P(msg_header_offset(msg, http, http_te), &http->http_te); TEST_P(msg_header_offset(msg, http, http_authorization), &http->http_authorization); TEST_P(msg_header_offset(msg, http, http_www_authenticate), &http->http_www_authenticate); TEST_P(msg_header_offset(msg, http, http_proxy_authenticate), &http->http_proxy_authenticate); TEST_P(msg_header_offset(msg, http, http_proxy_authorization), &http->http_proxy_authorization); TEST_P(msg_header_offset(msg, http, http_age), &http->http_age); TEST_P(msg_header_offset(msg, http, http_cache_control), &http->http_cache_control); TEST_P(msg_header_offset(msg, http, http_date), &http->http_date); TEST_P(msg_header_offset(msg, http, http_expires), &http->http_expires); TEST_P(msg_header_offset(msg, http, http_if_match), &http->http_if_match); TEST_P(msg_header_offset(msg, http, http_if_modified_since), &http->http_if_modified_since); TEST_P(msg_header_offset(msg, http, http_if_none_match), &http->http_if_none_match); TEST_P(msg_header_offset(msg, http, http_if_range), &http->http_if_range); TEST_P(msg_header_offset(msg, http, http_if_unmodified_since), &http->http_if_unmodified_since); TEST_P(msg_header_offset(msg, http, http_etag), &http->http_etag); TEST_P(msg_header_offset(msg, http, http_expect), &http->http_expect); TEST_P(msg_header_offset(msg, http, http_last_modified), &http->http_last_modified); TEST_P(msg_header_offset(msg, http, http_location), &http->http_location); TEST_P(msg_header_offset(msg, http, http_max_forwards), &http->http_max_forwards); TEST_P(msg_header_offset(msg, http, http_pragma), &http->http_pragma); TEST_P(msg_header_offset(msg, http, http_range), &http->http_range); TEST_P(msg_header_offset(msg, http, http_retry_after), &http->http_retry_after); TEST_P(msg_header_offset(msg, http, http_trailer), &http->http_trailer); TEST_P(msg_header_offset(msg, http, http_upgrade), &http->http_upgrade); TEST_P(msg_header_offset(msg, http, http_vary), &http->http_vary); TEST_P(msg_header_offset(msg, http, http_warning), &http->http_warning); TEST_P(msg_header_offset(msg, http, http_user_agent), &http->http_user_agent); TEST_P(msg_header_offset(msg, http, http_server), &http->http_server); TEST_P(msg_header_offset(msg, http, http_mime_version), &http->http_mime_version); TEST_P(msg_header_offset(msg, http, http_content_language), &http->http_content_language); TEST_P(msg_header_offset(msg, http, http_content_location), &http->http_content_location); TEST_P(msg_header_offset(msg, http, http_content_md5), &http->http_content_md5); TEST_P(msg_header_offset(msg, http, http_content_range), &http->http_content_range); TEST_P(msg_header_offset(msg, http, http_content_encoding), &http->http_content_encoding); TEST_P(msg_header_offset(msg, http, http_transfer_encoding), &http->http_transfer_encoding); TEST_P(msg_header_offset(msg, http, http_content_type), &http->http_content_type); TEST_P(msg_header_offset(msg, http, http_content_length), &http->http_content_length); TEST_SIZE(http_via_class->hc_params, offsetof(http_via_t, v_common)); TEST_SIZE(http_host_class->hc_params, offsetof(http_host_t, h_common)); TEST_SIZE(http_from_class->hc_params, offsetof(http_from_t, g_common)); TEST_SIZE(http_referer_class->hc_params, offsetof(http_referer_t, loc_common)); TEST_SIZE(http_connection_class->hc_params, offsetof(http_connection_t, k_items)); TEST_SIZE(http_accept_class->hc_params, offsetof(http_accept_t, ac_params)); TEST_SIZE(http_accept_charset_class->hc_params, offsetof(http_accept_charset_t, aa_params)); TEST_SIZE(http_accept_encoding_class->hc_params, offsetof(http_accept_encoding_t, aa_params)); TEST_SIZE(http_accept_language_class->hc_params, offsetof(http_accept_language_t, aa_params)); TEST_SIZE(http_accept_ranges_class->hc_params, offsetof(http_accept_ranges_t, k_items)); TEST_SIZE(http_allow_class->hc_params, offsetof(http_allow_t, k_items)); TEST_SIZE(http_te_class->hc_params, offsetof(http_te_t, te_params)); TEST_SIZE(http_authorization_class->hc_params, offsetof(http_authorization_t, au_params)); TEST_SIZE(http_www_authenticate_class->hc_params, offsetof(http_www_authenticate_t, au_params)); TEST_SIZE(http_proxy_authenticate_class->hc_params, offsetof(http_proxy_authenticate_t, au_params)); TEST_SIZE(http_proxy_authorization_class->hc_params, offsetof(http_proxy_authorization_t, au_params)); TEST_SIZE(http_age_class->hc_params, offsetof(http_age_t, x_common)); TEST_SIZE(http_cache_control_class->hc_params, offsetof(http_cache_control_t, k_items)); TEST_SIZE(http_date_class->hc_params, offsetof(http_date_t, d_common)); TEST_SIZE(http_expires_class->hc_params, offsetof(http_expires_t, d_common)); TEST_SIZE(http_if_match_class->hc_params, offsetof(http_if_match_t, k_items)); TEST_SIZE(http_if_modified_since_class->hc_params, offsetof(http_if_modified_since_t, d_common)); TEST_SIZE(http_if_none_match_class->hc_params, offsetof(http_if_none_match_t, k_items)); TEST_SIZE(http_if_range_class->hc_params, offsetof(http_if_range_t, ifr_common)); TEST_SIZE(http_if_unmodified_since_class->hc_params, offsetof(http_if_unmodified_since_t, d_common)); TEST_SIZE(http_etag_class->hc_params, offsetof(http_etag_t, g_common)); TEST_SIZE(http_expect_class->hc_params, offsetof(http_expect_t, g_common)); TEST_SIZE(http_last_modified_class->hc_params, offsetof(http_last_modified_t, d_common)); TEST_SIZE(http_location_class->hc_params, offsetof(http_location_t, loc_common)); TEST_SIZE(http_max_forwards_class->hc_params, offsetof(http_max_forwards_t, mf_common)); TEST_SIZE(http_pragma_class->hc_params, offsetof(http_pragma_t, k_items)); TEST_SIZE(http_range_class->hc_params, offsetof(http_range_t, rng_specs)); TEST_SIZE(http_retry_after_class->hc_params, offsetof(http_retry_after_t, ra_common)); TEST_SIZE(http_trailer_class->hc_params, offsetof(http_trailer_t, k_items)); TEST_SIZE(http_upgrade_class->hc_params, offsetof(http_upgrade_t, k_items)); TEST_SIZE(http_vary_class->hc_params, offsetof(http_vary_t, k_items)); TEST_SIZE(http_warning_class->hc_params, offsetof(http_warning_t, w_common)); TEST_SIZE(http_user_agent_class->hc_params, offsetof(http_user_agent_t, g_common)); TEST_SIZE(http_server_class->hc_params, offsetof(http_server_t, g_common)); TEST_SIZE(http_mime_version_class->hc_params, offsetof(http_mime_version_t, g_common)); TEST_SIZE(http_content_language_class->hc_params, offsetof(http_content_language_t, k_items)); TEST_SIZE(http_content_location_class->hc_params, offsetof(http_content_location_t, g_common)); TEST_SIZE(http_content_md5_class->hc_params, offsetof(http_content_md5_t, g_common)); TEST_SIZE(http_content_range_class->hc_params, offsetof(http_content_range_t, cr_common)); TEST_SIZE(http_content_encoding_class->hc_params, offsetof(http_content_encoding_t, k_items)); TEST_SIZE(http_transfer_encoding_class->hc_params, offsetof(http_transfer_encoding_t, k_items)); TEST_SIZE(http_content_type_class->hc_params, offsetof(http_content_type_t, c_params)); TEST_SIZE(http_content_length_class->hc_params, offsetof(http_content_length_t, l_common)); su_home_unref(home); END();}int count(msg_common_t *h){ http_header_t *sh = (http_header_t *)h; unsigned n; for (n = 0; sh; sh = sh->sh_next) n++; return n;}int len(msg_common_t *h){ msg_header_t *sh = (msg_header_t *)h; unsigned n; for (n = 0; sh; sh = sh->sh_next) { if (n) n +=2; n += msg_header_field_e(NULL, 0, sh, 0); } return n;}static int http_header_test(void){ su_home_t home[1] = { SU_HOME_INIT(home) }; BEGIN(); { http_request_t *rq; TEST_1(rq = http_request_make(home, "GET / HTTP/1.0")); TEST(rq->rq_method, http_method_get); TEST_S(rq->rq_method_name, "GET"); TEST_S(rq->rq_url->url_path, ""); TEST_1(rq->rq_url->url_root); TEST_S(url_as_string(home, rq->rq_url), "/"); TEST_P(rq->rq_version, http_version_1_0); TEST_1(rq = http_request_make(home, "GET / HTTP/1.2")); TEST(rq->rq_method, http_method_get); TEST_S(rq->rq_method_name, "GET"); TEST_S(rq->rq_url->url_path, ""); TEST_1(rq->rq_url->url_root); TEST_S(url_as_string(home, rq->rq_url), "/"); TEST_S(rq->rq_version, "HTTP/1.2"); TEST_1(rq = http_request_make(home, "GET /foo")); TEST(rq->rq_method, http_method_get); TEST_S(rq->rq_method_name, "GET"); TEST_S(rq->rq_url->url_path, "foo"); TEST_1(rq->rq_url->url_root); TEST_S(url_as_string(home, rq->rq_url), "/foo"); TEST_S(rq->rq_version, ""); TEST_P(rq->rq_version, http_version_0_9); } { http_status_t *st; st = http_status_make(home, "HTTP/1.0 100 Continue"); TEST_1(st); TEST_S(st->st_version, "HTTP/1.0"); TEST_P(st->st_version, http_version_1_0); TEST(st->st_status, 100); TEST_S(st->st_phrase, "Continue"); st = http_status_make(home, "HTTP/1.1 200"); TEST_1(st); TEST_S(st->st_version, "HTTP/1.1"); TEST(st->st_status, 200); TEST_S(st->st_phrase, ""); st = http_status_make(home, "HTTP/1.1 200 Ok"); TEST_1(st); TEST_S(st->st_version, "HTTP/1.1"); TEST_P(st->st_version, http_version_1_1); TEST(st->st_status, 200); TEST_S(st->st_phrase, "Ok"); st = http_status_make(home, "HTTP 99 Ok "); TEST_1(st); TEST_S(st->st_version, "HTTP"); TEST(st->st_status, 99); TEST_S(st->st_phrase, "Ok"); st = http_status_make(home, "HTTP/1.2 200 Ok"); TEST_1(st); TEST_S(st->st_version, "HTTP/1.2"); TEST(st->st_status, 200); TEST_S(st->st_phrase, "Ok"); } { http_content_range_t *cr; cr = http_content_range_make(home, "bytes 0 - 499 / *"); TEST_1(cr); TEST64(cr->cr_first, 0); TEST64(cr->cr_last, 499); TEST64(cr->cr_length, (http_off_t)-1); cr = http_content_range_make(home, "bytes 500-999/9913133"); TEST_1(cr); TEST64(cr->cr_first, 500); TEST64(cr->cr_last, 999); TEST64(cr->cr_length, 9913133); TEST_1(!http_content_range_make(home, "bytes = 0 - 499 / *,")); } { http_cookie_t *c; c = http_cookie_make(home, "$Version=1;" "foo=bar;$Domain=.nokia.com;$Path=\"\""); TEST_1(c); TEST_1(c->c_params); TEST_S(c->c_version, "1"); TEST_S(c->c_name, "foo=bar"); TEST_S(c->c_domain, ".nokia.com"); TEST_S(c->c_path, "\"\""); c = http_cookie_make(home, "$Version=1;" "foo=bar;$Domain=.nokia.com;$Path=\"\", , " "bar=bazzz;$Domain=.research.nokia.com;$Path=\"\";" "hum=ham;$Domain=.nokia.fi;$Path=\"/sofia\"" ); TEST_1(c); TEST_1(c->c_params); TEST_S(c->c_version, "1"); TEST_S(c->c_name, "foo=bar"); TEST_S(c->c_domain, ".nokia.com"); TEST_S(c->c_path, "\"\""); c = http_cookie_make(home, "foo=bar=baz1"); TEST_1(c); TEST_1(c->c_params); TEST_S(c->c_params[0], "foo=bar=baz1"); } { http_set_cookie_t *sc; sc = http_set_cookie_make(home, "foo=bar;Domain=.nokia.com;Path=\"\"" ";Foo=bar;Version=1;Secure;Max-age=1212;" "Comment=\"Jummi Jammmi\""); TEST_1(sc); TEST_1(sc->sc_params); TEST_S(sc->sc_name, "foo=bar"); TEST_S(sc->sc_version, "1"); TEST_S(sc->sc_domain, ".nokia.com"); TEST_S(sc->sc_max_age, "1212"); TEST_S(sc->sc_path, "\"\""); TEST_S(sc->sc_comment, "\"Jummi Jammmi\""); TEST(sc->sc_secure, 1); sc = http_set_cookie_make(home, "foo=bar;Domain=.nokia.com;Path=\"\"" ";Foo=bar;Version=1"); TEST_1(sc); TEST_1(sc->sc_params); TEST_S(sc->sc_name, "foo=bar"); TEST_S(sc->sc_version, "1"); TEST_S(sc->sc_domain, ".nokia.com"); TEST_P(sc->sc_max_age, NULL); TEST_S(sc->sc_path, "\"\""); TEST_S(sc->sc_comment, NULL); TEST(sc->sc_secure, 0); sc = http_set_cookie_make(home, "CUSTOMER=WILE_E_COYOTE; " "path=/; " "expires=Wednesday, 09-Nov-99 23:12:40 GMT"); TEST_1(sc); TEST_1(sc->sc_params); TEST_S(sc->sc_name, "CUSTOMER=WILE_E_COYOTE"); TEST_S(sc->sc_version, NULL); TEST_S(sc->sc_domain, NULL); TEST_S(sc->sc_max_age, NULL); TEST_S(sc->sc_path, "/");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -