|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package snapshot |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "io" |
| 10 | + gohttp "net/http" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func Test_checkResponse(t *testing.T) { |
| 19 | + type args struct { |
| 20 | + resp *gohttp.Response |
| 21 | + } |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + args args |
| 25 | + wantErr assert.ErrorAssertionFunc |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "Valid http response", |
| 29 | + args: args{ |
| 30 | + resp: &gohttp.Response{ |
| 31 | + Status: "OK", |
| 32 | + StatusCode: gohttp.StatusOK, |
| 33 | + Header: map[string][]string{ |
| 34 | + "Content-Type": {"application/json; charset=UTF-8"}, |
| 35 | + }, |
| 36 | + Body: io.NopCloser(strings.NewReader(`{"good": "job"}`)), |
| 37 | + }, |
| 38 | + }, |
| 39 | + wantErr: assert.NoError, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Bad http status code - 500", |
| 43 | + args: args{ |
| 44 | + resp: &gohttp.Response{ |
| 45 | + Status: "Not OK", |
| 46 | + StatusCode: gohttp.StatusInternalServerError, |
| 47 | + Header: map[string][]string{ |
| 48 | + "Content-Type": {"application/json"}, |
| 49 | + }, |
| 50 | + Body: io.NopCloser(strings.NewReader(`{"not feeling": "too well"}`)), |
| 51 | + }, |
| 52 | + }, |
| 53 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 54 | + return assert.ErrorContains(t, err, strconv.Itoa(gohttp.StatusInternalServerError), "error should contain http status code") && |
| 55 | + assert.ErrorContains(t, err, `{"not feeling": "too well"}`, "error should contain response body") |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Bad http status code - 502", |
| 60 | + args: args{ |
| 61 | + resp: &gohttp.Response{ |
| 62 | + Status: "Bad Gateway", |
| 63 | + StatusCode: gohttp.StatusBadGateway, |
| 64 | + Header: map[string][]string{ |
| 65 | + "Content-Type": {"application/json; charset=UTF-8"}, |
| 66 | + }, |
| 67 | + Body: io.NopCloser(strings.NewReader(`{"bad": "gateway"}`)), |
| 68 | + }, |
| 69 | + }, |
| 70 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 71 | + return assert.ErrorContains(t, err, strconv.Itoa(gohttp.StatusBadGateway), "error should contain http status code") && |
| 72 | + assert.ErrorContains(t, err, `{"bad": "gateway"}`, "error should contain response body") |
| 73 | + }, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "Bad http status code - 503", |
| 77 | + args: args{ |
| 78 | + resp: &gohttp.Response{ |
| 79 | + Status: "Service Unavailable", |
| 80 | + StatusCode: gohttp.StatusServiceUnavailable, |
| 81 | + Header: map[string][]string{ |
| 82 | + "Content-Type": {"application/json"}, |
| 83 | + }, |
| 84 | + Body: io.NopCloser(bytes.NewReader([]byte{})), |
| 85 | + }, |
| 86 | + }, |
| 87 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 88 | + return assert.ErrorContains(t, err, strconv.Itoa(gohttp.StatusServiceUnavailable), "error should contain http status code") |
| 89 | + }, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "Bad http status code - 504", |
| 93 | + args: args{ |
| 94 | + resp: &gohttp.Response{ |
| 95 | + Status: "Gateway timed out", |
| 96 | + StatusCode: gohttp.StatusGatewayTimeout, |
| 97 | + Header: map[string][]string{ |
| 98 | + "Content-Type": {"application/json; charset=UTF-8"}, |
| 99 | + }, |
| 100 | + Body: io.NopCloser(strings.NewReader(`{"gateway": "never got back to me"}`)), |
| 101 | + }, |
| 102 | + }, |
| 103 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 104 | + return assert.ErrorContains(t, err, strconv.Itoa(gohttp.StatusGatewayTimeout), "error should contain http status code") && |
| 105 | + assert.ErrorContains(t, err, `{"gateway": "never got back to me"}`, "error should contain response body") |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "Wrong content type: XML", |
| 110 | + args: args{ |
| 111 | + resp: &gohttp.Response{ |
| 112 | + Status: "XML is back in, baby", |
| 113 | + StatusCode: gohttp.StatusOK, |
| 114 | + Header: map[string][]string{ |
| 115 | + "Content-Type": {"application/xml"}, |
| 116 | + }, |
| 117 | + Body: io.NopCloser(strings.NewReader(`<?xml version='1.0' encoding='UTF-8'?><note>Those who cannot remember the past are condemned to repeat it.</note>`)), |
| 118 | + }, |
| 119 | + }, |
| 120 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 121 | + return assert.ErrorContains(t, err, "application/xml") && |
| 122 | + assert.ErrorContains(t, err, `<?xml version='1.0' encoding='UTF-8'?><note>Those who cannot remember the past are condemned to repeat it.</note>`) |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "Wrong content type: HTML", |
| 127 | + args: args{ |
| 128 | + resp: &gohttp.Response{ |
| 129 | + Status: "HTML is always (not) machine-friendly", |
| 130 | + StatusCode: gohttp.StatusOK, |
| 131 | + Header: map[string][]string{ |
| 132 | + "Content-Type": {"text/html"}, |
| 133 | + }, |
| 134 | + Body: io.NopCloser(strings.NewReader(`<!DOCTYPE html><html><body>Hello world!</body></html>`)), |
| 135 | + }, |
| 136 | + }, |
| 137 | + wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 138 | + return assert.ErrorContains(t, err, "text/html") && |
| 139 | + assert.ErrorContains(t, err, `<!DOCTYPE html><html><body>Hello world!</body></html>`) |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + for _, tt := range tests { |
| 144 | + t.Run(tt.name, func(t *testing.T) { |
| 145 | + err := checkResponse(tt.args.resp) |
| 146 | + if !tt.wantErr(t, err) { |
| 147 | + return |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
0 commit comments