comparison grpc_request_buffering.t @ 1311:4979af9fd905

Tests: grpc request buffering and next upstream tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 29 Mar 2018 18:35:02 +0300
parents
children 351b95be742b
comparison
equal deleted inserted replaced
1310:3882f8f3b2bc 1311:4979af9fd905
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for grpc module, request body buffered.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19 use Test::Nginx::HTTP2;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 my $t = Test::Nginx->new()->has(qw/http http_v2 grpc mirror/);
27
28 $t->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 http {
38 %%TEST_GLOBALS_HTTP%%
39
40 server {
41 listen 127.0.0.1:8080 http2;
42 server_name localhost;
43
44 location /mirror { }
45
46 location / {
47 grpc_pass 127.0.0.1:8081;
48 add_header X-Body $request_body;
49 mirror /mirror;
50 }
51 }
52 }
53
54 EOF
55
56 $t->try_run('no grpc')->plan(9);
57
58 ###############################################################################
59
60 my $p = port(8081);
61 my $f = grpc();
62
63 my $frames = $f->{http_start}('/SayHello');
64 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
65 is($frame->{flags}, 4, 'request - HEADERS flags');
66 is($frame->{headers}{':method'}, 'POST', 'request - method');
67 is($frame->{headers}{':scheme'}, 'http', 'request - scheme');
68 is($frame->{headers}{':path'}, '/SayHello', 'request - path');
69 is($frame->{headers}{':authority'}, "127.0.0.1:$p", 'request - authority');
70
71 ($frame) = grep { $_->{type} eq "DATA" } @$frames;
72 is($frame->{data}, 'Hello', 'request - DATA');
73 is($frame->{length}, 5, 'request - DATA length');
74 is($frame->{flags}, 1, 'request - DATA flags');
75
76 $frames = $f->{http_end}();
77 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
78 is($frame->{headers}{'x-body'}, 'Hello', 'request body in memory');
79
80 ###############################################################################
81
82 sub grpc {
83 my ($server, $client, $f, $s, $c, $sid, $uri);
84
85 $server = IO::Socket::INET->new(
86 Proto => 'tcp',
87 LocalHost => '127.0.0.1',
88 LocalPort => $p,
89 Listen => 5,
90 Reuse => 1
91 )
92 or die "Can't create listening socket: $!\n";
93
94 $f->{http_start} = sub {
95 ($uri, my %extra) = @_;
96 $s = Test::Nginx::HTTP2->new() if !defined $s;
97 $s->new_stream({ body => 'Hello', headers => [
98 { name => ':method', value => 'POST', mode => 0 },
99 { name => ':scheme', value => 'http', mode => 0 },
100 { name => ':path', value => $uri },
101 { name => ':authority', value => 'localhost' },
102 { name => 'content-length', value => '5' }]});
103
104 if (!$extra{reuse}) {
105 $client = $server->accept() or return;
106 log2c("(new connection $client)");
107
108 $client->sysread(my $buf, 24) == 24 or return; # preface
109
110 $c = Test::Nginx::HTTP2->new(1, socket => $client,
111 pure => 1, preface => "") or return;
112 }
113
114 my $frames = $c->read(all => [{ fin => 1 }]);
115
116 if (!$extra{reuse}) {
117 $c->h2_settings(0);
118 $c->h2_settings(1);
119 }
120
121 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
122 $sid = $frame->{sid};
123 return $frames;
124 };
125 $f->{http_end} = sub {
126 $c->new_stream({ body_more => 1, headers => [
127 { name => ':status', value => '200', mode => 0 },
128 { name => 'content-type', value => 'application/grpc' },
129 ]}, $sid);
130 $c->h2_body('Hello world', { body_more => 1 });
131 $c->new_stream({ headers => [
132 { name => 'grpc-status', value => '0', mode => 2 },
133 { name => 'grpc-message', value => '', mode => 2 },
134 ]}, $sid);
135
136 return $s->read(all => [{ fin => 1 }]);
137 };
138 return $f;
139 }
140
141 sub log2c { Test::Nginx::log_core('||', @_); }
142
143 ###############################################################################