|
1 | 1 | package api |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
4 | 6 | "fmt" |
5 | | - "github.com/stretchr/testify/require" |
6 | 7 | "mokapi/config/static" |
| 8 | + "mokapi/providers/openapi" |
7 | 9 | "mokapi/runtime" |
8 | 10 | "mokapi/runtime/events" |
9 | 11 | "mokapi/runtime/events/eventstest" |
10 | 12 | "mokapi/try" |
11 | 13 | "net/http" |
| 14 | + "net/http/httptest" |
12 | 15 | "testing" |
13 | 16 | "time" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/require" |
14 | 19 | ) |
15 | 20 |
|
16 | 21 | func TestHandler_Events(t *testing.T) { |
@@ -73,7 +78,109 @@ func TestHandler_Events(t *testing.T) { |
73 | 78 | }, |
74 | 79 | }, |
75 | 80 | { |
76 | | - name: "get specific event but not existing", |
| 81 | + name: "get http event with header parameter as string", |
| 82 | + fn: func(t *testing.T, h http.Handler, sm *events.StoreManager) { |
| 83 | + sm.SetStore(1, events.NewTraits().WithNamespace("http")) |
| 84 | + |
| 85 | + r := httptest.NewRequest("get", "http://localhost/foo", nil) |
| 86 | + r.Header.Set("foo", "bar") |
| 87 | + |
| 88 | + params := &openapi.RequestParameters{Header: map[string]openapi.RequestParameterValue{}} |
| 89 | + v := "bar" |
| 90 | + params.Header["Foo"] = openapi.RequestParameterValue{ |
| 91 | + Value: "bar", |
| 92 | + Raw: &v, |
| 93 | + } |
| 94 | + r = r.WithContext(openapi.NewContext(context.Background(), params)) |
| 95 | + |
| 96 | + _, err := openapi.NewLogEventContext(r, false, sm, events.NewTraits().WithNamespace("http")) |
| 97 | + require.NoError(t, err) |
| 98 | + try.Handler(t, |
| 99 | + http.MethodGet, |
| 100 | + "http://foo.api/api/events?namespace=http", |
| 101 | + nil, |
| 102 | + "", |
| 103 | + h, |
| 104 | + try.HasStatusCode(200), |
| 105 | + try.AssertBody(func(t *testing.T, body string) { |
| 106 | + var m []map[string]any |
| 107 | + require.NoError(t, json.Unmarshal([]byte(body), &m)) |
| 108 | + require.Equal(t, map[string]interface{}{ |
| 109 | + "actions": interface{}(nil), |
| 110 | + "api": "", |
| 111 | + "deprecated": false, |
| 112 | + "duration": float64(0), |
| 113 | + "path": "", |
| 114 | + "request": map[string]interface{}{ |
| 115 | + "method": "get", |
| 116 | + "parameters": []interface{}{ |
| 117 | + map[string]interface{}{ |
| 118 | + "name": "Foo", |
| 119 | + "raw": "bar", |
| 120 | + "type": "header", |
| 121 | + "value": "\"bar\"", |
| 122 | + }, |
| 123 | + }, |
| 124 | + "url": "http://localhost/foo", |
| 125 | + }, |
| 126 | + "response": map[string]interface{}{"body": "", "size": float64(0), "statusCode": float64(0)}}, |
| 127 | + m[0]["data"]) |
| 128 | + })) |
| 129 | + }, |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "get http event with header parameter as object", |
| 133 | + fn: func(t *testing.T, h http.Handler, sm *events.StoreManager) { |
| 134 | + sm.SetStore(1, events.NewTraits().WithNamespace("http")) |
| 135 | + |
| 136 | + r := httptest.NewRequest("get", "http://localhost/foo", nil) |
| 137 | + r.Header.Set("foo", "role,admin,firstName,Alex") |
| 138 | + |
| 139 | + params := &openapi.RequestParameters{Header: map[string]openapi.RequestParameterValue{}} |
| 140 | + v := "bar" |
| 141 | + params.Header["Foo"] = openapi.RequestParameterValue{ |
| 142 | + Value: map[string]any{"role": "admin", "firstName": "Alex"}, |
| 143 | + Raw: &v, |
| 144 | + } |
| 145 | + r = r.WithContext(openapi.NewContext(context.Background(), params)) |
| 146 | + |
| 147 | + _, err := openapi.NewLogEventContext(r, false, sm, events.NewTraits().WithNamespace("http")) |
| 148 | + require.NoError(t, err) |
| 149 | + try.Handler(t, |
| 150 | + http.MethodGet, |
| 151 | + "http://foo.api/api/events?namespace=http", |
| 152 | + nil, |
| 153 | + "", |
| 154 | + h, |
| 155 | + try.HasStatusCode(200), |
| 156 | + try.AssertBody(func(t *testing.T, body string) { |
| 157 | + var m []map[string]any |
| 158 | + require.NoError(t, json.Unmarshal([]byte(body), &m)) |
| 159 | + require.Equal(t, map[string]interface{}{ |
| 160 | + "actions": interface{}(nil), |
| 161 | + "api": "", |
| 162 | + "deprecated": false, |
| 163 | + "duration": float64(0), |
| 164 | + "path": "", |
| 165 | + "request": map[string]interface{}{ |
| 166 | + "method": "get", |
| 167 | + "parameters": []interface{}{ |
| 168 | + map[string]interface{}{ |
| 169 | + "name": "Foo", |
| 170 | + "raw": "bar", |
| 171 | + "type": "header", |
| 172 | + "value": "{\"firstName\":\"Alex\",\"role\":\"admin\"}", |
| 173 | + }, |
| 174 | + }, |
| 175 | + "url": "http://localhost/foo", |
| 176 | + }, |
| 177 | + "response": map[string]interface{}{"body": "", "size": float64(0), "statusCode": float64(0)}}, |
| 178 | + m[0]["data"]) |
| 179 | + })) |
| 180 | + }, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "http with request parameter", |
77 | 184 | fn: func(t *testing.T, h http.Handler, sm *events.StoreManager) { |
78 | 185 | try.Handler(t, |
79 | 186 | http.MethodGet, |
|
0 commit comments