LCOV - code coverage report
Current view: top level - test/core/httpcli - format_request_test.c (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 58 58 100.0 %
Date: 2015-10-10 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include "src/core/httpcli/format_request.h"
      35             : 
      36             : #include <string.h>
      37             : 
      38             : #include <grpc/support/log.h>
      39             : #include "test/core/util/test_config.h"
      40             : 
      41           1 : static void test_format_get_request(void) {
      42           1 :   grpc_httpcli_header hdr = {"x-yz", "abc"};
      43             :   grpc_httpcli_request req;
      44             :   gpr_slice slice;
      45             : 
      46           1 :   memset(&req, 0, sizeof(req));
      47           1 :   req.host = "example.com";
      48           1 :   req.path = "/index.html";
      49           1 :   req.hdr_count = 1;
      50           1 :   req.hdrs = &hdr;
      51             : 
      52           1 :   slice = grpc_httpcli_format_get_request(&req);
      53             : 
      54           1 :   GPR_ASSERT(0 == gpr_slice_str_cmp(slice,
      55             :                                     "GET /index.html HTTP/1.0\r\n"
      56             :                                     "Host: example.com\r\n"
      57             :                                     "Connection: close\r\n"
      58             :                                     "User-Agent: " GRPC_HTTPCLI_USER_AGENT
      59             :                                     "\r\n"
      60             :                                     "x-yz: abc\r\n"
      61             :                                     "\r\n"));
      62             : 
      63           1 :   gpr_slice_unref(slice);
      64           1 : }
      65             : 
      66           1 : static void test_format_post_request(void) {
      67           1 :   grpc_httpcli_header hdr = {"x-yz", "abc"};
      68             :   grpc_httpcli_request req;
      69             :   gpr_slice slice;
      70           1 :   char body_bytes[] = "fake body";
      71           1 :   size_t body_len = 9;
      72             : 
      73           1 :   memset(&req, 0, sizeof(req));
      74           1 :   req.host = "example.com";
      75           1 :   req.path = "/index.html";
      76           1 :   req.hdr_count = 1;
      77           1 :   req.hdrs = &hdr;
      78             : 
      79           1 :   slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);
      80             : 
      81           1 :   GPR_ASSERT(0 == gpr_slice_str_cmp(slice,
      82             :                                     "POST /index.html HTTP/1.0\r\n"
      83             :                                     "Host: example.com\r\n"
      84             :                                     "Connection: close\r\n"
      85             :                                     "User-Agent: " GRPC_HTTPCLI_USER_AGENT
      86             :                                     "\r\n"
      87             :                                     "x-yz: abc\r\n"
      88             :                                     "Content-Type: text/plain\r\n"
      89             :                                     "Content-Length: 9\r\n"
      90             :                                     "\r\n"
      91             :                                     "fake body"));
      92             : 
      93           1 :   gpr_slice_unref(slice);
      94           1 : }
      95             : 
      96           1 : static void test_format_post_request_no_body(void) {
      97           1 :   grpc_httpcli_header hdr = {"x-yz", "abc"};
      98             :   grpc_httpcli_request req;
      99             :   gpr_slice slice;
     100             : 
     101           1 :   memset(&req, 0, sizeof(req));
     102           1 :   req.host = "example.com";
     103           1 :   req.path = "/index.html";
     104           1 :   req.hdr_count = 1;
     105           1 :   req.hdrs = &hdr;
     106             : 
     107           1 :   slice = grpc_httpcli_format_post_request(&req, NULL, 0);
     108             : 
     109           1 :   GPR_ASSERT(0 == gpr_slice_str_cmp(slice,
     110             :                                     "POST /index.html HTTP/1.0\r\n"
     111             :                                     "Host: example.com\r\n"
     112             :                                     "Connection: close\r\n"
     113             :                                     "User-Agent: " GRPC_HTTPCLI_USER_AGENT
     114             :                                     "\r\n"
     115             :                                     "x-yz: abc\r\n"
     116             :                                     "\r\n"));
     117             : 
     118           1 :   gpr_slice_unref(slice);
     119           1 : }
     120             : 
     121           1 : static void test_format_post_request_content_type_override(void) {
     122             :   grpc_httpcli_header hdrs[2];
     123             :   grpc_httpcli_request req;
     124             :   gpr_slice slice;
     125           1 :   char body_bytes[] = "fake%20body";
     126           1 :   size_t body_len = 11;
     127             : 
     128           1 :   hdrs[0].key = "x-yz";
     129           1 :   hdrs[0].value = "abc";
     130           1 :   hdrs[1].key = "Content-Type";
     131           1 :   hdrs[1].value = "application/x-www-form-urlencoded";
     132           1 :   memset(&req, 0, sizeof(req));
     133           1 :   req.host = "example.com";
     134           1 :   req.path = "/index.html";
     135           1 :   req.hdr_count = 2;
     136           1 :   req.hdrs = hdrs;
     137             : 
     138           1 :   slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);
     139             : 
     140           1 :   GPR_ASSERT(0 == gpr_slice_str_cmp(
     141             :                       slice,
     142             :                       "POST /index.html HTTP/1.0\r\n"
     143             :                       "Host: example.com\r\n"
     144             :                       "Connection: close\r\n"
     145             :                       "User-Agent: " GRPC_HTTPCLI_USER_AGENT
     146             :                       "\r\n"
     147             :                       "x-yz: abc\r\n"
     148             :                       "Content-Type: application/x-www-form-urlencoded\r\n"
     149             :                       "Content-Length: 11\r\n"
     150             :                       "\r\n"
     151             :                       "fake%20body"));
     152             : 
     153           1 :   gpr_slice_unref(slice);
     154           1 : }
     155             : 
     156           1 : int main(int argc, char **argv) {
     157           1 :   grpc_test_init(argc, argv);
     158             : 
     159           1 :   test_format_get_request();
     160           1 :   test_format_post_request();
     161           1 :   test_format_post_request_no_body();
     162           1 :   test_format_post_request_content_type_override();
     163             : 
     164           1 :   return 0;
     165             : }

Generated by: LCOV version 1.10