Skip to content

Commit c7dde22

Browse files
authored
Merge pull request #30 from TykTechnologies/TT-13378
Returning 404 when field not found
2 parents e1d0ec1 + d50a0fd commit c7dde22

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

.github/workflows/ci-test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
fetch-depth: 2
1717

1818
- name: Setup Golang
19-
uses: actions/setup-go@v2
19+
uses: actions/setup-go@v4
2020
with:
2121
go-version-file: go.mod
2222

exporter.go

+54-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,30 @@ func (v *Viewer) ConfigHandler(rw http.ResponseWriter, r *http.Request) {
2020
}
2121

2222
rw.Header().Set("Content-type", "application/json")
23-
rw.WriteHeader(http.StatusOK)
23+
24+
if configField := r.URL.Query().Get(JSONQueryKey); configField != "" {
25+
response := v.EnvNotation(configField)
26+
if response.Value == nil {
27+
rw.WriteHeader(http.StatusNotFound)
28+
29+
err := json.NewEncoder(rw).Encode(map[string]string{
30+
"error": "field not found",
31+
})
32+
if err != nil {
33+
return
34+
}
35+
36+
return
37+
}
38+
39+
err := json.NewEncoder(rw).Encode(response)
40+
if err != nil {
41+
rw.WriteHeader(http.StatusInternalServerError)
42+
return
43+
}
44+
45+
return
46+
}
2447

2548
err := json.NewEncoder(rw).Encode(v.config)
2649
if err != nil {
@@ -37,15 +60,27 @@ func (v *Viewer) DetailedConfigHandler(rw http.ResponseWriter, r *http.Request)
3760
}
3861

3962
rw.Header().Set("Content-type", "application/json")
40-
rw.WriteHeader(http.StatusOK)
4163

4264
if configField := r.URL.Query().Get(JSONQueryKey); configField != "" {
43-
err := json.NewEncoder(rw).Encode(v.EnvNotation(configField))
65+
response := v.EnvNotation(configField)
66+
if response.Value == nil {
67+
rw.WriteHeader(http.StatusNotFound)
68+
69+
err := json.NewEncoder(rw).Encode(map[string]string{
70+
"error": "field not found",
71+
})
72+
if err != nil {
73+
return
74+
}
75+
76+
return
77+
}
78+
79+
err := json.NewEncoder(rw).Encode(response)
4480
if err != nil {
4581
rw.WriteHeader(http.StatusInternalServerError)
4682
return
4783
}
48-
4984
return
5085
}
5186

@@ -64,15 +99,27 @@ func (v *Viewer) EnvsHandler(rw http.ResponseWriter, r *http.Request) {
6499
}
65100

66101
rw.Header().Set("Content-type", "application/json")
67-
rw.WriteHeader(http.StatusOK)
68102

69103
if env := r.URL.Query().Get(EnvQueryKey); env != "" {
70-
err := json.NewEncoder(rw).Encode(v.JSONNotation(env))
104+
response := v.JSONNotation(env)
105+
if response.Value == nil {
106+
rw.WriteHeader(http.StatusNotFound)
107+
108+
err := json.NewEncoder(rw).Encode(map[string]string{
109+
"error": "environment variable not found",
110+
})
111+
if err != nil {
112+
return
113+
}
114+
115+
return
116+
}
117+
118+
err := json.NewEncoder(rw).Encode(response)
71119
if err != nil {
72120
rw.WriteHeader(http.StatusInternalServerError)
73121
return
74122
}
75-
76123
return
77124
}
78125

exporter_test.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func TestConfigHandler(t *testing.T) {
7474
expectedJSONOutput string
7575

7676
shouldDeleteConfig bool
77+
queryParamVal string
7778
}{
7879
{
7980
testName: "simple struct",
@@ -98,6 +99,15 @@ func TestConfigHandler(t *testing.T) {
9899
expectedJSONOutput: "",
99100
shouldDeleteConfig: true,
100101
},
102+
{
103+
testName: "invalid field",
104+
givenConfig: complexStruct,
105+
expectedStatusCode: http.StatusNotFound,
106+
expectedJSONOutput: toJSON(t, map[string]string{
107+
"error": "field not found",
108+
}),
109+
queryParamVal: "invalid_field",
110+
},
101111
}
102112

103113
for _, tc := range tcs {
@@ -107,6 +117,8 @@ func TestConfigHandler(t *testing.T) {
107117
req, err := http.NewRequest("GET", "/", nil)
108118
assert.NoError(t, err)
109119

120+
setQueryParams(req, JSONQueryKey, tc.queryParamVal)
121+
110122
structViewerConfig := Config{Object: tc.givenConfig}
111123
helper, err := New(&structViewerConfig, "TYK_")
112124
assert.NoError(t, err, "failed to instantiate viewer")
@@ -196,8 +208,10 @@ func TestDetailedConfigHandler(t *testing.T) {
196208
testName: "invalid field complexStruct via query param",
197209
givenConfig: complexStruct,
198210
queryParamVal: "data.object_3",
199-
expectedStatusCode: http.StatusOK,
200-
expectedJSONOutput: toJSON(t, EnvVar{}),
211+
expectedStatusCode: http.StatusNotFound,
212+
expectedJSONOutput: toJSON(t, map[string]string{
213+
"error": "field not found",
214+
}),
201215
},
202216
{
203217
testName: "not initialized",
@@ -321,8 +335,10 @@ func TestEnvsHandler(t *testing.T) {
321335
givenConfig: complexStruct,
322336
givenPrefix: "TYK_",
323337
queryParamVal: "TYK_DATA_OBJECT3",
324-
expectedStatusCode: http.StatusOK,
325-
expectedJSONOutput: toJSON(t, EnvVar{}),
338+
expectedStatusCode: http.StatusNotFound,
339+
expectedJSONOutput: toJSON(t, map[string]string{
340+
"error": "environment variable not found",
341+
}),
326342
},
327343
}
328344

0 commit comments

Comments
 (0)