Author: kotkov
Date: Wed Aug  9 14:49:27 2017
New Revision: 1804534

URL: http://svn.apache.org/viewvc?rev=1804534&view=rev
Log:
Add a test for an error while reading the request body for an outgoing
request.

Apparently, Serf 1.3.x has a few related problems, which were fixed in
trunk by the introduced pump layer.  This test should aid in preparing
the 1.3.x-specific fix for these issues.

* test/test_serf.h
  (test_request_setup_t): New typedef.
  (handler_baton_t): Add an optional test_request_setup_t.
  (create_new_request_with_resp_hdlr): Morph into ...
  (create_new_request_ex): ...this new function that allows setting up
   non-default request setup and response handlers.
  (setup_handler): Accept an optional test_request_setup_t.

* test/test_util.c
  (create_new_request_with_resp_hdlr): Morph into ...
  (create_new_request_ex): ...this new function that allows setting up
   non-default request setup and response handlers.
  (setup_handler): Accept an optional test_request_setup_t.
  (create_new_prio_request, create_new_request): Update calls to
   setup_handler().
  (setup_request): Use the custom test_request_setup_t if available.

* test/test_context.c
  (setup_request_err): New helper function, implements test_request_setup_t.
  (test_outgoing_request_err): New test.
  (test_context): Add new test.

Modified:
    serf/trunk/test/test_context.c
    serf/trunk/test/test_serf.h
    serf/trunk/test/test_util.c

Modified: serf/trunk/test/test_context.c
URL: 
http://svn.apache.org/viewvc/serf/trunk/test/test_context.c?rev=1804534&r1=1804533&r2=1804534&view=diff
==============================================================================
--- serf/trunk/test/test_context.c (original)
+++ serf/trunk/test/test_context.c Wed Aug  9 14:49:27 2017
@@ -456,8 +456,8 @@ static void test_keepalive_limit_one_by_
     EndGiven
 
     for (i = 0 ; i < SENT_REQUESTS ; i++) {
-        create_new_request_with_resp_hdlr(tb, &handler_ctx[i], "GET", "/", i+1,
-                                          handle_response_keepalive_limit);
+        create_new_request_ex(tb, &handler_ctx[i], "GET", "/", i+1,
+                              NULL, handle_response_keepalive_limit);
     }
 
     /* The two retries of request 1 both also have req_id=1, which means that
@@ -561,8 +561,8 @@ static void test_keepalive_limit_one_by_
     EndGiven
 
     for (i = 0 ; i < SENT_REQUESTS ; i++) {
-        create_new_request_with_resp_hdlr(tb, &handler_ctx[i], "GET", "/", i+1,
-                                          
handle_response_keepalive_limit_burst);
+        create_new_request_ex(tb, &handler_ctx[i], "GET", "/", i+1,
+                              NULL, handle_response_keepalive_limit_burst);
     }
 
     /* The two retries of request 1 both also have req_id=1, which means that
@@ -973,6 +973,50 @@ static void test_max_keepalive_requests(
     CuAssertIntEquals(tc, num_requests, tb->handled_requests->nelts);
 }
 
+/* Implements test_request_setup_t */
+static apr_status_t setup_request_err(serf_request_t *request,
+                                      void *setup_baton,
+                                      serf_bucket_t **req_bkt,
+                                      apr_pool_t *pool)
+{
+    static mockbkt_action actions[] = {
+        { 1, "a", APR_SUCCESS },
+        /* Return an error after first successful read. */
+        { 1, "", APR_EINVAL }
+    };
+    handler_baton_t *ctx = setup_baton;
+    serf_bucket_alloc_t *alloc;
+    serf_bucket_t *mock_bkt;
+
+    alloc = serf_request_get_alloc(request);
+    mock_bkt = serf_bucket_mock_create(actions, 2, alloc);
+    *req_bkt = serf_request_bucket_request_create(request,
+                                                  ctx->method, ctx->path,
+                                                  mock_bkt, alloc);
+    return APR_SUCCESS;
+}
+
+static void test_outgoing_request_err(CuTest *tc)
+{
+    test_baton_t *tb = tc->testBaton;
+    handler_baton_t handler_ctx[1];
+    apr_status_t status;
+
+    setup_test_mock_server(tb);
+    status = setup_test_client_context(tb, NULL, tb->pool);
+    CuAssertIntEquals(tc, APR_SUCCESS, status);
+
+    /* Setup an outgoing request with the body bucket returning an error. */
+    create_new_request_ex(tb, &handler_ctx[0], "POST", "/", 1,
+                          setup_request_err, NULL);
+
+    status = run_client_and_mock_servers_loops(tb, 1, handler_ctx, tb->pool);
+    CuAssertIntEquals(tc, APR_EINVAL, status);
+    CuAssertIntEquals(tc, 1, tb->sent_requests->nelts);
+    CuAssertIntEquals(tc, 0, tb->accepted_requests->nelts);
+    CuAssertIntEquals(tc, 0, tb->handled_requests->nelts);
+}
+
 /*****************************************************************************/
 CuSuite *test_context(void)
 {
@@ -998,6 +1042,7 @@ CuSuite *test_context(void)
     SUITE_ADD_TEST(suite, test_connection_large_response);
     SUITE_ADD_TEST(suite, test_connection_large_request);
     SUITE_ADD_TEST(suite, test_max_keepalive_requests);
+    SUITE_ADD_TEST(suite, test_outgoing_request_err);
 
     return suite;
 }

Modified: serf/trunk/test/test_serf.h
URL: 
http://svn.apache.org/viewvc/serf/trunk/test/test_serf.h?rev=1804534&r1=1804533&r2=1804534&view=diff
==============================================================================
--- serf/trunk/test/test_serf.h (original)
+++ serf/trunk/test/test_serf.h Wed Aug  9 14:49:27 2017
@@ -123,6 +123,13 @@ apr_status_t use_new_connection(test_bat
 void *test_setup(void *baton);
 void *test_teardown(void *baton);
 
+/* Simple variant of serf_request_setup_t for tests. */
+typedef apr_status_t (*test_request_setup_t)(
+    serf_request_t *request,
+    void *setup_baton,
+    serf_bucket_t **req_bkt,
+    apr_pool_t *pool);
+
 typedef struct handler_baton_t {
     serf_response_acceptor_t acceptor;
     void *acceptor_baton;
@@ -138,6 +145,8 @@ typedef struct handler_baton_t {
     const char *path;
     /* Use this for a raw request message */
     const char *request;
+    /* Or this, if more control is needed. */
+    test_request_setup_t request_setup;
     int done;
 
     test_baton_t *tb;
@@ -171,6 +180,7 @@ apr_status_t handle_response(serf_reques
 void setup_handler(test_baton_t *tb, handler_baton_t *handler_ctx,
                    const char *method, const char *path,
                    int req_id,
+                   test_request_setup_t req_setup,
                    serf_response_handler_t handler);
 void create_new_prio_request(test_baton_t *tb,
                              handler_baton_t *handler_ctx,
@@ -181,11 +191,12 @@ void create_new_request(test_baton_t *tb
                         const char *method, const char *path,
                         int req_id);
 void
-create_new_request_with_resp_hdlr(test_baton_t *tb,
-                                  handler_baton_t *handler_ctx,
-                                  const char *method, const char *path,
-                                  int req_id,
-                                  serf_response_handler_t handler);
+create_new_request_ex(test_baton_t *tb,
+                      handler_baton_t *handler_ctx,
+                      const char *method, const char *path,
+                      int req_id,
+                      test_request_setup_t req_setup,
+                      serf_response_handler_t handler);
 
 const char *create_large_response_message(apr_pool_t *pool);
 const char *create_large_request_message_body(apr_pool_t *pool);

Modified: serf/trunk/test/test_util.c
URL: 
http://svn.apache.org/viewvc/serf/trunk/test/test_util.c?rev=1804534&r1=1804533&r2=1804534&view=diff
==============================================================================
--- serf/trunk/test/test_util.c (original)
+++ serf/trunk/test/test_util.c Wed Aug  9 14:49:27 2017
@@ -191,8 +191,13 @@ apr_status_t setup_request(serf_request_
 {
     handler_baton_t *ctx = setup_baton;
     serf_bucket_t *body_bkt;
+    apr_status_t status = APR_SUCCESS;
 
-    if (ctx->request)
+    if (ctx->request_setup)
+    {
+        status = ctx->request_setup(request, setup_baton, req_bkt, pool);
+    }
+    else if (ctx->request)
     {
         /* Create a raw request bucket. */
         *req_bkt = serf_bucket_simple_create(ctx->request, 
strlen(ctx->request),
@@ -226,7 +231,7 @@ apr_status_t setup_request(serf_request_
     *handler = ctx->handler;
     *handler_baton = ctx;
 
-    return APR_SUCCESS;
+    return status;
 }
 
 apr_status_t handle_response(serf_request_t *request,
@@ -270,6 +275,7 @@ apr_status_t handle_response(serf_reques
 void setup_handler(test_baton_t *tb, handler_baton_t *handler_ctx,
                    const char *method, const char *path,
                    int req_id,
+                   test_request_setup_t req_setup,
                    serf_response_handler_t handler)
 {
     handler_ctx->method = method;
@@ -285,6 +291,7 @@ void setup_handler(test_baton_t *tb, han
     handler_ctx->handled_requests = tb->handled_requests;
     handler_ctx->tb = tb;
     handler_ctx->request = NULL;
+    handler_ctx->request_setup = req_setup;
 }
 
 void create_new_prio_request(test_baton_t *tb,
@@ -292,7 +299,7 @@ void create_new_prio_request(test_baton_
                              const char *method, const char *path,
                              int req_id)
 {
-    setup_handler(tb, handler_ctx, method, path, req_id, NULL);
+    setup_handler(tb, handler_ctx, method, path, req_id, NULL, NULL);
     serf_connection_priority_request_create(tb->connection,
                                             setup_request,
                                             handler_ctx);
@@ -303,20 +310,21 @@ void create_new_request(test_baton_t *tb
                         const char *method, const char *path,
                         int req_id)
 {
-    setup_handler(tb, handler_ctx, method, path, req_id, NULL);
+    setup_handler(tb, handler_ctx, method, path, req_id, NULL, NULL);
     serf_connection_request_create(tb->connection,
                                    setup_request,
                                    handler_ctx);
 }
 
 void
-create_new_request_with_resp_hdlr(test_baton_t *tb,
-                                  handler_baton_t *handler_ctx,
-                                  const char *method, const char *path,
-                                  int req_id,
-                                  serf_response_handler_t handler)
+create_new_request_ex(test_baton_t *tb,
+                      handler_baton_t *handler_ctx,
+                      const char *method, const char *path,
+                      int req_id,
+                      test_request_setup_t req_setup,
+                      serf_response_handler_t handler)
 {
-    setup_handler(tb, handler_ctx, method, path, req_id, handler);
+    setup_handler(tb, handler_ctx, method, path, req_id, req_setup, handler);
     serf_connection_request_create(tb->connection,
                                    setup_request,
                                    handler_ctx);


Reply via email to