From 8db7d0daa3427a476dded94c0ecb6442875f0d49 Mon Sep 17 00:00:00 2001 From: jvelilla Date: Fri, 10 May 2013 09:48:28 -0300 Subject: [PATCH] Initial implementation of HTTP RESPONSE EXPECTATIONS. Added a class to test http client with httpbin.org and expectations --- .../http_client_response_body_expectation.e | 48 +++++++ .../src/http_client_response_expectation.e | 27 ++++ ..._client_response_status_code_expectation.e | 42 ++++++ .../tests/test_http_client_with_httpbin.e | 121 ++++++++++++++++++ 4 files changed, 238 insertions(+) create mode 100644 library/network/http_client/src/http_client_response_body_expectation.e create mode 100644 library/network/http_client/src/http_client_response_expectation.e create mode 100644 library/network/http_client/src/http_client_response_status_code_expectation.e create mode 100644 library/network/http_client/tests/test_http_client_with_httpbin.e diff --git a/library/network/http_client/src/http_client_response_body_expectation.e b/library/network/http_client/src/http_client_response_body_expectation.e new file mode 100644 index 00000000..cf2039cf --- /dev/null +++ b/library/network/http_client/src/http_client_response_body_expectation.e @@ -0,0 +1,48 @@ +note + description: "Summary description for {HTTP_CLIENT_RESPONSE_BODY_EXPECTATION}." + author: "" + date: "$Date$" + revision: "$Revision$" + +class + HTTP_CLIENT_RESPONSE_BODY_EXPECTATION +inherit + HTTP_CLIENT_RESPONSE_EXPECTATION +create + make + +feature {NONE} -- Initializtion + make (a_body : detachable STRING_32) + -- Create and Initialize a body expectation + do + body := a_body + ensure + body_set : body ~ a_body + end + +feature -- Result expected + expected (resp: HTTP_CLIENT_RESPONSE): BOOLEAN + -- is `body expected' equals to resp.body? + do + if body = Void and then resp.body = Void then + Result := True + elseif attached resp.body as l_body and then attached body as c_body then + Result := l_body.is_case_insensitive_equal (c_body) + end + end + +feature -- Access + body : detachable STRING_32 + -- expected body + +;note + copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Eiffel Software and others" + license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" + source: "[ + Eiffel Software + 5949 Hollister Ave., Goleta, CA 93117 USA + Telephone 805-685-1006, Fax 805-685-6869 + Website http://www.eiffel.com + Customer support http://support.eiffel.com + ]" +end diff --git a/library/network/http_client/src/http_client_response_expectation.e b/library/network/http_client/src/http_client_response_expectation.e new file mode 100644 index 00000000..43741ec6 --- /dev/null +++ b/library/network/http_client/src/http_client_response_expectation.e @@ -0,0 +1,27 @@ +note + description: "Summary description for {HTTP_CLIENT_RESPONSE_EXPECTATION}." + author: "" + date: "$Date$" + revision: "$Revision$" + +deferred class + HTTP_CLIENT_RESPONSE_EXPECTATION + +feature + + expected (resp: HTTP_CLIENT_RESPONSE): BOOLEAN + deferred + end + +note + copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Eiffel Software and others" + license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" + source: "[ + Eiffel Software + 5949 Hollister Ave., Goleta, CA 93117 USA + Telephone 805-685-1006, Fax 805-685-6869 + Website http://www.eiffel.com + Customer support http://support.eiffel.com + ]" + +end diff --git a/library/network/http_client/src/http_client_response_status_code_expectation.e b/library/network/http_client/src/http_client_response_status_code_expectation.e new file mode 100644 index 00000000..bf829c90 --- /dev/null +++ b/library/network/http_client/src/http_client_response_status_code_expectation.e @@ -0,0 +1,42 @@ +note + description: "Summary description for {HTTP_CLIENT_RESPONSE_STATUS_CODE_EXPECTATION}." + author: "" + date: "$Date$" + revision: "$Revision$" + +class + HTTP_CLIENT_RESPONSE_STATUS_CODE_EXPECTATION +inherit + HTTP_CLIENT_RESPONSE_EXPECTATION + +create make + +feature {NONE} -- Initialization + make (a_status : INTEGER_32) + do + status := a_status + ensure + status_set : status = a_status + end + +feature -- Result Expected + expected (resp: HTTP_CLIENT_RESPONSE): BOOLEAN + -- is `status' expected equals to resp.status? + do + Result := status = resp.status + end + +feature -- Access + status : INTEGER_32 + -- status expected +;note + copyright: "2011-2013, Jocelyn Fiat, Javier Velilla, Eiffel Software and others" + license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)" + source: "[ + Eiffel Software + 5949 Hollister Ave., Goleta, CA 93117 USA + Telephone 805-685-1006, Fax 805-685-6869 + Website http://www.eiffel.com + Customer support http://support.eiffel.com + ]" +end diff --git a/library/network/http_client/tests/test_http_client_with_httpbin.e b/library/network/http_client/tests/test_http_client_with_httpbin.e new file mode 100644 index 00000000..e27d3570 --- /dev/null +++ b/library/network/http_client/tests/test_http_client_with_httpbin.e @@ -0,0 +1,121 @@ +note + description: "Summary description for {TEST_HTTP_CLIENT_WITH_HTTPBIN}." + author: "" + date: "$Date$" + revision: "$Revision$" + EIS: "name=HTTP TESTING", "protocol=uri", "src=http://httpbin.org/" + +class + TEST_HTTP_CLIENT_WITH_HTTPBIN + +inherit + EQA_TEST_SET + +feature + test_origin_ip + local + h: LIBCURL_HTTP_CLIENT + sess: HTTP_CLIENT_SESSION + resp : detachable HTTP_CLIENT_RESPONSE + l_location : detachable READABLE_STRING_8 + body : STRING + context : HTTP_CLIENT_REQUEST_CONTEXT + s: READABLE_STRING_8 + do + create h.make + sess := h.new_session ("http://httpbin.org/") + resp := sess.get ("",void) + assert ("Expected Status 200 ok", resp.status = 200) + end + + test_status_code + local + h: LIBCURL_HTTP_CLIENT + sess: HTTP_CLIENT_SESSION + resp : detachable HTTP_CLIENT_RESPONSE + l_location : detachable READABLE_STRING_8 + body : STRING + context : HTTP_CLIENT_REQUEST_CONTEXT + s: READABLE_STRING_8 + do + create h.make + sess := h.new_session ("http://httpbin.org/") + assert ("Expected Status 200 ok", (create {HTTP_CLIENT_RESPONSE_STATUS_CODE_EXPECTATION}.make(200)).expected(sess.get ("",void)) ) + end + + test_body + local + h: LIBCURL_HTTP_CLIENT + sess: HTTP_CLIENT_SESSION + resp : detachable HTTP_CLIENT_RESPONSE + l_location : detachable READABLE_STRING_8 + body : STRING + context : HTTP_CLIENT_REQUEST_CONTEXT + s: READABLE_STRING_8 + do + create h.make + sess := h.new_session ("http://httpbin.org/") + assert ("Expected no body", (create {HTTP_CLIENT_RESPONSE_BODY_EXPECTATION}.make(expected_body_get)).expected(sess.get ("get",void)) ) + end + + test_body_status + local + h: LIBCURL_HTTP_CLIENT + sess: HTTP_CLIENT_SESSION + resp : detachable HTTP_CLIENT_RESPONSE + l_location : detachable READABLE_STRING_8 + body : STRING + context : HTTP_CLIENT_REQUEST_CONTEXT + s: READABLE_STRING_8 + do + create h.make + sess := h.new_session ("http://httpbin.org/") + --assert ("Expected no body", (create {HTTP_CLIENT_RESPONSE_STATUS_CODE_EXPECTATION}.make(200) + create {HTTP_CLIENT_RESPONSE_BODY_EXPECTATION}.make(expected_body_get)).expected(sess.get ("get",void)) ) + end + + + test_after_post_should_continue_working + local + h: LIBCURL_HTTP_CLIENT + sess: HTTP_CLIENT_SESSION + resp : detachable HTTP_CLIENT_RESPONSE + l_location : detachable READABLE_STRING_8 + body : STRING + context : HTTP_CLIENT_REQUEST_CONTEXT + s: READABLE_STRING_8 + do + create h.make + sess := h.new_session ("http://httpbin.org/") + resp := sess.get ("",void) + assert ("Expected Status 200 ok", resp.status = 200) + + create context.make + context.headers.put ("text/plain;charset=UTF-8", "Content-Type") + resp := sess.post ("post", context, "testing post") + assert ("Expected Status 200 ok", resp.status = 200) + + sess := h.new_session ("http://httpbin.org/") + resp := sess.get ("",void) + assert ("Expected Status 200 ok", resp.status = 200) + + sess := h.new_session ("http://httpbin.org/") + resp := sess.get ("redirect/1",void) + assert ("Expected Status 200 ok", resp.status = 200) + + end + + + +expected_body_get : STRING_32 = "[ +{ + "url": "http://httpbin.org/get", + "args": {}, + "headers": { + "Host": "httpbin.org", + "Connection": "close", + "Accept": "*/*" + }, + "origin": "190.177.106.187" +} +]" +end