|
| 1 | +/* Copyright 2017 Istio Authors. All Rights Reserved. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +#include "test/integration/integration.h" |
| 17 | +#include "common/grpc/codec.h" |
| 18 | +#include "common/grpc/common.h" |
| 19 | +#include "src/envoy/transcoding/test/bookstore.pb.h" |
| 20 | + |
| 21 | +#include "gtest/gtest.h" |
| 22 | + |
| 23 | +class TranscodingIntegrationTest : public BaseIntegrationTest, |
| 24 | + public testing::Test { |
| 25 | + public: |
| 26 | + /** |
| 27 | + * Global initializer for all integration tests. |
| 28 | + */ |
| 29 | + void SetUp() override { |
| 30 | + fake_upstreams_.emplace_back( |
| 31 | + new FakeUpstream(0, FakeHttpConnection::Type::HTTP2)); |
| 32 | + registerPort("upstream_0", |
| 33 | + fake_upstreams_.back()->localAddress()->ip()->port()); |
| 34 | + createTestServer("src/envoy/transcoding/test/integration.json", {"http"}); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Global destructor for all integration tests. |
| 39 | + */ |
| 40 | + void TearDown() override { |
| 41 | + test_server_.reset(); |
| 42 | + fake_upstreams_.clear(); |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +TEST_F(TranscodingIntegrationTest, BasicUnary) { |
| 47 | + IntegrationCodecClientPtr codec_client; |
| 48 | + FakeHttpConnectionPtr fake_upstream_connection; |
| 49 | + IntegrationStreamDecoderPtr response( |
| 50 | + new IntegrationStreamDecoder(*dispatcher_)); |
| 51 | + |
| 52 | + codec_client = |
| 53 | + makeHttpConnection(lookupPort("http"), Http::CodecClient::Type::HTTP1); |
| 54 | + Http::StreamEncoder& encoder = codec_client->startRequest( |
| 55 | + Http::TestHeaderMapImpl{{":method", "POST"}, |
| 56 | + {":path", "/shelf"}, |
| 57 | + {":authority", "host"}, |
| 58 | + {"content-type", "application/json"}}, |
| 59 | + *response); |
| 60 | + Buffer::OwnedImpl request_data{"{\"theme\": \"Children\"}"}; |
| 61 | + codec_client->sendData(encoder, request_data, true); |
| 62 | + |
| 63 | + fake_upstream_connection = |
| 64 | + fake_upstreams_[0]->waitForHttpConnection(*dispatcher_); |
| 65 | + FakeStreamPtr request_stream = fake_upstream_connection->waitForNewStream(); |
| 66 | + |
| 67 | + request_stream->waitForEndStream(*dispatcher_); |
| 68 | + |
| 69 | + Grpc::Decoder grpc_decoder; |
| 70 | + std::vector<Grpc::Frame> frames; |
| 71 | + grpc_decoder.decode(request_stream->body(), frames); |
| 72 | + EXPECT_EQ(1, frames.size()); |
| 73 | + |
| 74 | + bookstore::CreateShelfRequest csr; |
| 75 | + csr.ParseFromArray(frames[0].data_->linearize(frames[0].length_), |
| 76 | + frames[0].length_); |
| 77 | + EXPECT_EQ("Children", csr.shelf().theme()); |
| 78 | + |
| 79 | + request_stream->encodeHeaders( |
| 80 | + Http::TestHeaderMapImpl{{"content-type", "application/grpc"}, |
| 81 | + {":status", "200"}}, |
| 82 | + false); |
| 83 | + |
| 84 | + bookstore::Shelf response_pb; |
| 85 | + response_pb.set_id(20); |
| 86 | + response_pb.set_theme("Children"); |
| 87 | + |
| 88 | + auto response_data = Grpc::Common::serializeBody(response_pb); |
| 89 | + request_stream->encodeData(*response_data, false); |
| 90 | + |
| 91 | + request_stream->encodeTrailers( |
| 92 | + Http::TestHeaderMapImpl{{"grpc-status", "0"}, {"grpc-message", ""}}); |
| 93 | + |
| 94 | + response->waitForEndStream(); |
| 95 | + EXPECT_TRUE(request_stream->complete()); |
| 96 | + EXPECT_TRUE(response->complete()); |
| 97 | + EXPECT_EQ("{\"id\":\"20\",\"theme\":\"Children\"}", response->body()); |
| 98 | + |
| 99 | + codec_client->close(); |
| 100 | + fake_upstream_connection->close(); |
| 101 | + fake_upstream_connection->waitForDisconnect(); |
| 102 | +} |
0 commit comments