|
| 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 | +package test |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | +) |
| 21 | + |
| 22 | +// Report attributes from a good GET request |
| 23 | +const reportAttributesOkGet = ` |
| 24 | +{ |
| 25 | + "request.host": "localhost:27070", |
| 26 | + "request.path": "/echo", |
| 27 | + "request.time": "*", |
| 28 | + "request.useragent": "Go-http-client/1.1", |
| 29 | + "request.method": "GET", |
| 30 | + "request.scheme": "http", |
| 31 | + "source.uid": "POD11", |
| 32 | + "source.namespace": "XYZ11", |
| 33 | + "target.uid": "POD222", |
| 34 | + "target.namespace": "XYZ222", |
| 35 | + "request.headers": { |
| 36 | + ":method": "GET", |
| 37 | + ":path": "/echo", |
| 38 | + ":authority": "localhost:27070", |
| 39 | + "x-forwarded-proto": "http", |
| 40 | + "x-istio-attributes": "-", |
| 41 | + "x-request-id": "*" |
| 42 | + }, |
| 43 | + "request.size": 0, |
| 44 | + "response.time": "*", |
| 45 | + "response.size": 0, |
| 46 | + "response.duration": "*", |
| 47 | + "response.code": 200, |
| 48 | + "response.headers": { |
| 49 | + "date": "*", |
| 50 | + "content-type": "text/plain; charset=utf-8", |
| 51 | + "content-length": "0", |
| 52 | + ":status": "200", |
| 53 | + "server": "envoy" |
| 54 | + } |
| 55 | +} |
| 56 | +` |
| 57 | + |
| 58 | +// Report attributes from a good POST request |
| 59 | +const reportAttributesOkPost1 = ` |
| 60 | +{ |
| 61 | + "request.host": "localhost:27070", |
| 62 | + "request.path": "/echo", |
| 63 | + "request.time": "*", |
| 64 | + "request.useragent": "Go-http-client/1.1", |
| 65 | + "request.method": "POST", |
| 66 | + "request.scheme": "http", |
| 67 | + "source.uid": "POD11", |
| 68 | + "source.namespace": "XYZ11", |
| 69 | + "target.uid": "POD222", |
| 70 | + "target.namespace": "XYZ222", |
| 71 | + "request.headers": { |
| 72 | + ":method": "POST", |
| 73 | + ":path": "/echo", |
| 74 | + ":authority": "localhost:27070", |
| 75 | + "x-forwarded-proto": "http", |
| 76 | + "x-istio-attributes": "-", |
| 77 | + "x-request-id": "*" |
| 78 | + }, |
| 79 | + "request.size": 12, |
| 80 | + "response.time": "*", |
| 81 | + "response.size": 12, |
| 82 | + "response.duration": "*", |
| 83 | + "response.code": 200, |
| 84 | + "response.headers": { |
| 85 | + "date": "*", |
| 86 | + "content-type": "text/plain", |
| 87 | + "content-length": "12", |
| 88 | + ":status": "200", |
| 89 | + "server": "envoy" |
| 90 | + } |
| 91 | +} |
| 92 | +` |
| 93 | + |
| 94 | +// Report attributes from a good POST request |
| 95 | +const reportAttributesOkPost2 = ` |
| 96 | +{ |
| 97 | + "request.host": "localhost:27070", |
| 98 | + "request.path": "/echo", |
| 99 | + "request.time": "*", |
| 100 | + "request.useragent": "Go-http-client/1.1", |
| 101 | + "request.method": "POST", |
| 102 | + "request.scheme": "http", |
| 103 | + "source.uid": "POD11", |
| 104 | + "source.namespace": "XYZ11", |
| 105 | + "target.uid": "POD222", |
| 106 | + "target.namespace": "XYZ222", |
| 107 | + "request.headers": { |
| 108 | + ":method": "POST", |
| 109 | + ":path": "/echo", |
| 110 | + ":authority": "localhost:27070", |
| 111 | + "x-forwarded-proto": "http", |
| 112 | + "x-istio-attributes": "-", |
| 113 | + "x-request-id": "*" |
| 114 | + }, |
| 115 | + "request.size": 18, |
| 116 | + "response.time": "*", |
| 117 | + "response.size": 18, |
| 118 | + "response.duration": "*", |
| 119 | + "response.code": 200, |
| 120 | + "response.headers": { |
| 121 | + "date": "*", |
| 122 | + "content-type": "text/plain", |
| 123 | + "content-length": "18", |
| 124 | + ":status": "200", |
| 125 | + "server": "envoy" |
| 126 | + } |
| 127 | +} |
| 128 | +` |
| 129 | + |
| 130 | +func TestReportBatch(t *testing.T) { |
| 131 | + s := &TestSetup{ |
| 132 | + t: t, |
| 133 | + conf: basicConfig, |
| 134 | + } |
| 135 | + if err := s.SetUp(); err != nil { |
| 136 | + t.Fatalf("Failed to setup test: %v", err) |
| 137 | + } |
| 138 | + defer s.TearDown() |
| 139 | + |
| 140 | + url := fmt.Sprintf("http://localhost:%d/echo", ClientProxyPort) |
| 141 | + |
| 142 | + // Issues a GET echo request with 0 size body |
| 143 | + tag := "OKGet" |
| 144 | + if _, _, err := HTTPGet(url); err != nil { |
| 145 | + t.Errorf("Failed in request %s: %v", tag, err) |
| 146 | + } |
| 147 | + // Issues a POST request. |
| 148 | + tag = "OKPost1" |
| 149 | + if _, _, err := HTTPPost(url, "text/plain", "Hello World!"); err != nil { |
| 150 | + t.Errorf("Failed in request %s: %v", tag, err) |
| 151 | + } |
| 152 | + // Issues a POST request again. |
| 153 | + tag = "OKPost2" |
| 154 | + if _, _, err := HTTPPost(url, "text/plain", "Hello World Again!"); err != nil { |
| 155 | + t.Errorf("Failed in request %s: %v", tag, err) |
| 156 | + } |
| 157 | + tag = "Batch" |
| 158 | + s.VerifyReport(tag, reportAttributesOkGet) |
| 159 | + s.VerifyReport(tag, reportAttributesOkPost1) |
| 160 | + s.VerifyReport(tag, reportAttributesOkPost2) |
| 161 | +} |
0 commit comments