|
| 1 | +/* |
| 2 | + * TencentBlueKing is pleased to support the open source community by making |
| 3 | + * 蓝鲸智云 - API 网关(BlueKing - APIGateway) available. |
| 4 | + * Copyright (C) Tencent. All rights reserved. |
| 5 | + * Licensed under the MIT License (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://opensource.org/licenses/MIT |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 11 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 12 | + * either express or implied. See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + * |
| 15 | + * We undertake not to change the open source license (MIT license) applicable |
| 16 | + * to the current version of the project delivered to anyone in the future. |
| 17 | + */ |
| 18 | + |
| 19 | +package proxy |
| 20 | + |
| 21 | +import ( |
| 22 | + "errors" |
| 23 | + "io" |
| 24 | + "strconv" |
| 25 | + "strings" |
| 26 | + |
| 27 | + . "github.com/onsi/ginkgo/v2" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("readResponseBody", func() { |
| 32 | + DescribeTable("reads the complete response body regardless of the Content-Length hint", |
| 33 | + func(contentLength, body string) { |
| 34 | + result, err := readResponseBody(strings.NewReader(body), contentLength) |
| 35 | + |
| 36 | + Expect(err).NotTo(HaveOccurred()) |
| 37 | + Expect(result).To(Equal([]byte(body))) |
| 38 | + }, |
| 39 | + Entry("with an exact length", "13", "complete-body"), |
| 40 | + Entry("without a length", "", "complete-body"), |
| 41 | + Entry("with an invalid length", "not-a-number", "complete-body"), |
| 42 | + Entry("with a zero length", "0", "complete-body"), |
| 43 | + Entry("with a negative length", "-1", "complete-body"), |
| 44 | + Entry("with a length above the preallocation limit", |
| 45 | + strconv.Itoa(maxResponseBodyPreallocateSize+1), "complete-body"), |
| 46 | + Entry("with an underestimated length", "3", "complete-body"), |
| 47 | + Entry("with an overestimated length", "1024", "complete-body"), |
| 48 | + ) |
| 49 | + |
| 50 | + It("returns bytes read before a reader error and preserves the error", func() { |
| 51 | + readErr := errors.New("response read failed") |
| 52 | + reader := &errorResponseReader{ |
| 53 | + body: []byte("partial-body"), |
| 54 | + err: readErr, |
| 55 | + } |
| 56 | + |
| 57 | + result, err := readResponseBody(reader, strconv.Itoa(len(reader.body))) |
| 58 | + |
| 59 | + Expect(result).To(Equal([]byte("partial-body"))) |
| 60 | + Expect(err).To(MatchError(readErr)) |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +type errorResponseReader struct { |
| 65 | + body []byte |
| 66 | + err error |
| 67 | +} |
| 68 | + |
| 69 | +func (r *errorResponseReader) Read(p []byte) (int, error) { |
| 70 | + if len(r.body) == 0 { |
| 71 | + return 0, r.err |
| 72 | + } |
| 73 | + |
| 74 | + n := copy(p, r.body) |
| 75 | + r.body = r.body[n:] |
| 76 | + if len(r.body) == 0 { |
| 77 | + return n, r.err |
| 78 | + } |
| 79 | + return n, nil |
| 80 | +} |
| 81 | + |
| 82 | +var _ io.Reader = (*errorResponseReader)(nil) |
0 commit comments