Skip to content

Commit 54f7886

Browse files
authored
Merge pull request #6 from cto-ai/handle-null-config-values
Add null-handling to GetConfig and test it.
2 parents 09e10e0 + 5e1cc3c commit 54f7886

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

sdk.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ func (s *Sdk) GetConfig(key string) (string, error) {
8888
return "", err
8989
}
9090

91+
if daemonValue == nil {
92+
return "", nil
93+
}
94+
9195
stringValue, ok := daemonValue.(string)
9296
if !ok {
9397
return "", fmt.Errorf("Received non-string JSON %v", daemonValue)

state_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,42 @@ func TestGetConfig(t *testing.T) {
111111
}
112112
}
113113

114+
func TestGetConfig_Null(t *testing.T) {
115+
expectedResponse := `{"value": null}`
116+
expectedBody := map[string]interface{}{
117+
"key": "test-key",
118+
}
119+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
120+
ValidateRequest(t, r, "/config/get")
121+
122+
var tmp map[string]interface{}
123+
err := json.NewDecoder(r.Body).Decode(&tmp)
124+
if err != nil {
125+
t.Errorf("Error in decoding response body: %s", err)
126+
}
127+
128+
if !reflect.DeepEqual(tmp, expectedBody) {
129+
t.Errorf("Error unexpected request body: %+v", tmp)
130+
}
131+
132+
fmt.Fprintf(w, expectedResponse)
133+
}))
134+
135+
defer ts.Close()
136+
137+
SetPortVar(t, ts)
138+
139+
s := NewSdk()
140+
output, err := s.GetConfig("test-key")
141+
if err != nil {
142+
t.Errorf("Error in config request: %v", err)
143+
}
144+
145+
if output != "" {
146+
t.Errorf("Error unexpected output: %v", output)
147+
}
148+
}
149+
114150
func TestSetConfig(t *testing.T) {
115151
expectedBody := map[string]interface{}{
116152
"key": "key-of-value",

0 commit comments

Comments
 (0)