|
| 1 | +package aurestverifier |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "sort" |
| 13 | + "strings" |
| 14 | +) |
| 15 | + |
| 16 | +type VerifierImpl struct { |
| 17 | + expectations []Expectation |
| 18 | + firstUnexpected *Request |
| 19 | +} |
| 20 | + |
| 21 | +type Request struct { |
| 22 | + Name string // key for the request |
| 23 | + Method string |
| 24 | + Header http.Header // currently not tested, just supplied as documentation for now |
| 25 | + Url string |
| 26 | + Body interface{} |
| 27 | +} |
| 28 | + |
| 29 | +type ResponseOrError struct { |
| 30 | + Response aurestclientapi.ParsedResponse |
| 31 | + Error error |
| 32 | +} |
| 33 | + |
| 34 | +type Expectation struct { |
| 35 | + Request Request |
| 36 | + Response ResponseOrError |
| 37 | + Matched bool |
| 38 | +} |
| 39 | + |
| 40 | +func (e Expectation) matches(method string, requestUrl string, requestBody interface{}) bool { |
| 41 | + // this is a very simple "must match 100%" for the first version |
| 42 | + urlMatches := e.Request.Url == requestUrl |
| 43 | + methodMatches := e.Request.Method == method |
| 44 | + bodyMatches := requestBodyAsString(e.Request.Body) == requestBodyAsString(requestBody) |
| 45 | + |
| 46 | + return urlMatches && methodMatches && bodyMatches |
| 47 | +} |
| 48 | + |
| 49 | +func requestBodyAsString(requestBody interface{}) string { |
| 50 | + if requestBody == nil { |
| 51 | + return "" |
| 52 | + } |
| 53 | + if asCustom, ok := requestBody.(aurestclientapi.CustomRequestBody); ok { |
| 54 | + if b, err := io.ReadAll(asCustom.BodyReader); err == nil { |
| 55 | + return string(b) |
| 56 | + } else { |
| 57 | + return fmt.Sprintf("ERROR: %s", err.Error()) |
| 58 | + } |
| 59 | + } |
| 60 | + if asString, ok := requestBody.(string); ok { |
| 61 | + return asString |
| 62 | + } |
| 63 | + if asUrlValues, ok := requestBody.(url.Values); ok { |
| 64 | + asString := asUrlValues.Encode() |
| 65 | + return asString |
| 66 | + } |
| 67 | + |
| 68 | + marshalled, err := json.Marshal(requestBody) |
| 69 | + if err != nil { |
| 70 | + return fmt.Sprintf("ERROR: %s", err.Error()) |
| 71 | + } |
| 72 | + return string(marshalled) |
| 73 | +} |
| 74 | + |
| 75 | +func headersSortedAsString(spec http.Header) string { |
| 76 | + var result strings.Builder |
| 77 | + |
| 78 | + sortedKeys := make([]string, 0) |
| 79 | + for k, _ := range spec { |
| 80 | + sortedKeys = append(sortedKeys, k) |
| 81 | + } |
| 82 | + sort.Strings(sortedKeys) |
| 83 | + |
| 84 | + for _, k := range sortedKeys { |
| 85 | + result.WriteString(fmt.Sprintf("%s: ", k)) |
| 86 | + for _, v := range spec[k] { |
| 87 | + result.WriteString(fmt.Sprintf("%s ", v)) |
| 88 | + } |
| 89 | + } |
| 90 | + return result.String() |
| 91 | +} |
| 92 | + |
| 93 | +func New() (aurestclientapi.Client, *VerifierImpl) { |
| 94 | + instance := &VerifierImpl{ |
| 95 | + expectations: make([]Expectation, 0), |
| 96 | + } |
| 97 | + return instance, instance |
| 98 | +} |
| 99 | + |
| 100 | +func (c *VerifierImpl) Perform(ctx context.Context, method string, requestUrl string, requestBody interface{}, response *aurestclientapi.ParsedResponse) error { |
| 101 | + expected, err := c.currentExpectation(method, requestUrl, requestBody) |
| 102 | + if err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + if expected.Response.Error != nil { |
| 107 | + return expected.Response.Error |
| 108 | + } |
| 109 | + |
| 110 | + mockResponse := expected.Response.Response |
| 111 | + |
| 112 | + response.Header = mockResponse.Header |
| 113 | + response.Status = mockResponse.Status |
| 114 | + response.Time = mockResponse.Time |
| 115 | + if response.Body != nil && mockResponse.Body != nil { |
| 116 | + // copy over through json round trip |
| 117 | + marshalled, _ := json.Marshal(mockResponse.Body) |
| 118 | + _ = json.Unmarshal(marshalled, response.Body) |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (c *VerifierImpl) currentExpectation(method string, requestUrl string, requestBody interface{}) (Expectation, error) { |
| 125 | + for i, e := range c.expectations { |
| 126 | + if !e.Matched { |
| 127 | + if e.matches(method, requestUrl, requestBody) { |
| 128 | + c.expectations[i].Matched = true |
| 129 | + return e, nil |
| 130 | + } else { |
| 131 | + if c.firstUnexpected == nil { |
| 132 | + c.firstUnexpected = &Request{ |
| 133 | + Name: fmt.Sprintf("unmatched expectation %d - %s", i+1, e.Request.Name), |
| 134 | + Method: method, |
| 135 | + Header: nil, // not currently available |
| 136 | + Url: requestUrl, |
| 137 | + Body: requestBody, |
| 138 | + } |
| 139 | + } |
| 140 | + return Expectation{}, fmt.Errorf("unmatched expectation %d - %s", i+1, e.Request.Name) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if c.firstUnexpected == nil { |
| 146 | + c.firstUnexpected = &Request{ |
| 147 | + Name: fmt.Sprintf("no expectations remaining - unexpected request at end"), |
| 148 | + Method: method, |
| 149 | + Header: nil, // not currently available |
| 150 | + Url: requestUrl, |
| 151 | + Body: requestBody, |
| 152 | + } |
| 153 | + } |
| 154 | + return Expectation{}, errors.New("no expectations remaining - unexpected request at end") |
| 155 | +} |
| 156 | + |
| 157 | +func (c *VerifierImpl) AddExpectation(requestMatcher Request, response aurestclientapi.ParsedResponse, err error) { |
| 158 | + c.expectations = append(c.expectations, Expectation{ |
| 159 | + Request: requestMatcher, |
| 160 | + Response: ResponseOrError{ |
| 161 | + Response: response, |
| 162 | + Error: err, |
| 163 | + }, |
| 164 | + }) |
| 165 | +} |
| 166 | + |
| 167 | +func (c *VerifierImpl) FirstUnexpectedOrNil() *Request { |
| 168 | + return c.firstUnexpected |
| 169 | +} |
0 commit comments