|
| 1 | +// Use and distribution licensed under the Apache license version 2. |
| 2 | +// |
| 3 | +// See the COPYING file in the root project directory for full text. |
| 4 | + |
| 5 | +package http |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "context" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + nethttp "net/http" |
| 14 | + "reflect" |
| 15 | + "strings" |
| 16 | + |
| 17 | + gdtcontext "github.com/gdt-dev/core/context" |
| 18 | + "github.com/gdt-dev/core/debug" |
| 19 | +) |
| 20 | + |
| 21 | +// Action describes the the HTTP-specific action that is performed by the test. |
| 22 | +type Action struct { |
| 23 | + // URL being called by HTTP client. Used with the `Method` field. |
| 24 | + URL string `yaml:"url,omitempty"` |
| 25 | + // HTTP Method specified by HTTP client. Used with the `URL` shortcut field. |
| 26 | + Method string `yaml:"method,omitempty"` |
| 27 | + // Data is the payload to send along in request |
| 28 | + Data interface{} `yaml:"data,omitempty"` |
| 29 | + // Shortcut for URL and Method of "GET" |
| 30 | + Get string `yaml:"get,omitempty"` |
| 31 | + // Shortcut for URL and Method of "POST" |
| 32 | + Post string `yaml:"post,omitempty"` |
| 33 | + // Shortcut for URL and Method of "PUT" |
| 34 | + Put string `yaml:"put,omitempty"` |
| 35 | + // Shortcut for URL and Method of "PATCH" |
| 36 | + Patch string `yaml:"patch,omitempty"` |
| 37 | + // Shortcut for URL and Method of "DELETE" |
| 38 | + Delete string `yaml:"delete,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +// Do performs a single HTTP request, returning the HTTP Response and any |
| 42 | +// runtime error. |
| 43 | +func (a *Action) Do( |
| 44 | + ctx context.Context, |
| 45 | + c *nethttp.Client, |
| 46 | + defaults *Defaults, |
| 47 | +) (*nethttp.Response, error) { |
| 48 | + url, err := a.getURL(ctx, defaults) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + debug.Printf(ctx, "http: > %s %s", a.Method, url) |
| 54 | + var reqData io.Reader |
| 55 | + if a.Data != nil { |
| 56 | + if err := a.processRequestData(ctx); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + jsonBody, err := json.Marshal(a.Data) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + b := bytes.NewReader(jsonBody) |
| 64 | + if b.Size() > 0 { |
| 65 | + sendData, _ := io.ReadAll(b) |
| 66 | + debug.Printf(ctx, "http: > %s", sendData) |
| 67 | + b.Seek(0, 0) // nolint:errcheck |
| 68 | + } |
| 69 | + reqData = b |
| 70 | + } |
| 71 | + |
| 72 | + req, err := nethttp.NewRequest(a.Method, url, reqData) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + resp, err := c.Do(req) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + debug.Printf(ctx, "http: < %d", resp.StatusCode) |
| 82 | + return resp, err |
| 83 | +} |
| 84 | + |
| 85 | +// getURL returns the URL to use for the test's HTTP request. The test's url |
| 86 | +// field is first queried to see if it is the special $LOCATION string. If it |
| 87 | +// is, then we return the previous HTTP response's Location header. Otherwise, |
| 88 | +// we construct the URL from the httpFile's base URL and the test's url field. |
| 89 | +func (a *Action) getURL( |
| 90 | + ctx context.Context, |
| 91 | + defaults *Defaults, |
| 92 | +) (string, error) { |
| 93 | + if strings.ToUpper(a.URL) == "$LOCATION" { |
| 94 | + pr := priorRunData(ctx) |
| 95 | + if pr == nil || pr.Response == nil { |
| 96 | + panic("test unit referenced $LOCATION before executing an HTTP request") |
| 97 | + } |
| 98 | + url, err := pr.Response.Location() |
| 99 | + if err != nil { |
| 100 | + return "", ErrExpectedLocationHeader |
| 101 | + } |
| 102 | + return url.String(), nil |
| 103 | + } |
| 104 | + base := defaults.BaseURLFromContext(ctx) |
| 105 | + return base + a.URL, nil |
| 106 | +} |
| 107 | + |
| 108 | +// processRequestData looks through the raw data interface{} that was |
| 109 | +// unmarshaled during parse for any string values that look like JSONPath |
| 110 | +// expressions. If we find any, we query the fixture registry to see if any |
| 111 | +// fixtures have a value that matches the JSONPath expression. See |
| 112 | +// gdt.fixtures:jsonFixture for more information on how this works |
| 113 | +func (a *Action) processRequestData(ctx context.Context) error { |
| 114 | + if a.Data == nil { |
| 115 | + return nil |
| 116 | + } |
| 117 | + // Get a pointer to the unmarshaled interface{} so we can mutate the |
| 118 | + // contents pointed to |
| 119 | + p := reflect.ValueOf(&a.Data) |
| 120 | + |
| 121 | + // We're interested in the value pointed to by the interface{}, which is |
| 122 | + // why we do a double Elem() here. |
| 123 | + v := p.Elem().Elem() |
| 124 | + vt := v.Type() |
| 125 | + |
| 126 | + switch vt.Kind() { |
| 127 | + case reflect.Slice: |
| 128 | + for i := 0; i < v.Len(); i++ { |
| 129 | + item := v.Index(i).Elem() |
| 130 | + it := item.Type() |
| 131 | + err := a.preprocessMap(ctx, item, it.Key(), it.Elem()) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + } |
| 136 | + case reflect.Map: |
| 137 | + err := a.preprocessMap(ctx, v, vt.Key(), vt.Elem()) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +// processRequestDataMap processes a map pointed to by v, transforming any |
| 146 | +// string keys or values of the map into the results of calling the fixture |
| 147 | +// set's State() method. |
| 148 | +func (a *Action) preprocessMap( |
| 149 | + ctx context.Context, |
| 150 | + m reflect.Value, |
| 151 | + kt reflect.Type, |
| 152 | + vt reflect.Type, |
| 153 | +) error { |
| 154 | + it := m.MapRange() |
| 155 | + for it.Next() { |
| 156 | + if kt.Kind() == reflect.String { |
| 157 | + keyStr := it.Key().String() |
| 158 | + fixtures := gdtcontext.Fixtures(ctx) |
| 159 | + for _, f := range fixtures { |
| 160 | + if !f.HasState(keyStr) { |
| 161 | + continue |
| 162 | + } |
| 163 | + trKeyStr := f.State(keyStr) |
| 164 | + keyStr = trKeyStr.(string) |
| 165 | + } |
| 166 | + |
| 167 | + val := it.Value() |
| 168 | + err := a.preprocessMapValue(ctx, m, reflect.ValueOf(keyStr), val, val.Type()) |
| 169 | + if err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +func (a *Action) preprocessMapValue( |
| 178 | + ctx context.Context, |
| 179 | + m reflect.Value, |
| 180 | + k reflect.Value, |
| 181 | + v reflect.Value, |
| 182 | + vt reflect.Type, |
| 183 | +) error { |
| 184 | + if vt.Kind() == reflect.Interface { |
| 185 | + v = v.Elem() |
| 186 | + vt = v.Type() |
| 187 | + } |
| 188 | + |
| 189 | + switch vt.Kind() { |
| 190 | + case reflect.Slice: |
| 191 | + for i := 0; i < v.Len(); i++ { |
| 192 | + item := v.Index(i) |
| 193 | + fmt.Println(item) |
| 194 | + } |
| 195 | + fmt.Printf("map element is an array.\n") |
| 196 | + case reflect.Map: |
| 197 | + return a.preprocessMap(ctx, v, vt.Key(), vt.Elem()) |
| 198 | + case reflect.String: |
| 199 | + valStr := v.String() |
| 200 | + fixtures := gdtcontext.Fixtures(ctx) |
| 201 | + for _, f := range fixtures { |
| 202 | + if !f.HasState(valStr) { |
| 203 | + continue |
| 204 | + } |
| 205 | + trValStr := f.State(valStr) |
| 206 | + m.SetMapIndex(k, reflect.ValueOf(trValStr)) |
| 207 | + } |
| 208 | + default: |
| 209 | + return nil |
| 210 | + } |
| 211 | + return nil |
| 212 | +} |
0 commit comments