@@ -23,7 +23,6 @@ import (
2323 "net/http"
2424 "net/http/httptest"
2525 "net/url"
26- "reflect"
2726 "regexp"
2827 "strings"
2928 "sync"
@@ -165,13 +164,13 @@ func TestWaitToolEndpoints(t *testing.T) {
165164 name : "successful operation" ,
166165 toolName : "wait-for-op1" ,
167166 body : `{"project": "p1", "location": "l1", "operation": "op1"}` ,
168- want : `{"name":"op1","done":true ,"response":"success"}` ,
167+ want : `{"done":true,"name":"op1" ,"response":"success"}` ,
169168 },
170169 {
171- name : "failed operation" ,
172- toolName : "wait-for-op2" ,
173- body : `{"project": "p1", "location": "l1", "operation": "op2"}` ,
174- expectError : true ,
170+ name : "failed operation" ,
171+ toolName : "wait-for-op2" ,
172+ body : `{"project": "p1", "location": "l1", "operation": "op2"}` ,
173+ want : `{"error":"error processing request: operation finished with error: {\"code\":1,\"message\":\"failed\"}"}` ,
175174 },
176175 }
177176
@@ -189,48 +188,42 @@ func TestWaitToolEndpoints(t *testing.T) {
189188 }
190189 defer resp .Body .Close ()
191190
192- if tc .expectError {
193- if resp .StatusCode == http .StatusOK {
194- t .Fatal ("expected error but got status 200" )
195- }
196- return
197- }
198-
199191 if resp .StatusCode != http .StatusOK {
200192 bodyBytes , _ := io .ReadAll (resp .Body )
201193 t .Fatalf ("response status code is not 200, got %d: %s" , resp .StatusCode , string (bodyBytes ))
202194 }
203-
204- var result struct {
205- Result string `json:"result"`
195+ var response struct {
196+ Result any `json:"result"`
206197 }
207- if err := json .NewDecoder (resp .Body ).Decode (& result ); err != nil {
198+ if err := json .NewDecoder (resp .Body ).Decode (& response ); err != nil {
208199 t .Fatalf ("failed to decode response: %v" , err )
209200 }
210201
211- if tc .wantSubstring {
212- if ! bytes .Contains ([]byte (result .Result ), []byte (tc .want )) {
213- t .Fatalf ("unexpected result: got %q, want substring %q" , result .Result , tc .want )
202+ var got string
203+ // Check if the result is a string (which contains JSON)
204+ if s , ok := response .Result .(string ); ok {
205+ got = s
206+ } else {
207+ b , err := json .Marshal (response .Result )
208+ if err != nil {
209+ t .Fatalf ("failed to marshal result object: %v" , err )
214210 }
215- return
211+ got = string ( b )
216212 }
217213
218- // The result is a JSON-encoded string, so we need to unmarshal it twice.
219- var tempString string
220- if err := json .Unmarshal ([]byte (result .Result ), & tempString ); err != nil {
221- t .Fatalf ("failed to unmarshal result string: %v" , err )
222- }
214+ // Clean up both strings to ignore whitespace differences
215+ got = strings .ReplaceAll (strings .ReplaceAll (got , " " , "" ), "\n " , "" )
216+ want := strings .ReplaceAll (strings .ReplaceAll (tc .want , " " , "" ), "\n " , "" )
223217
224- var got , want map [string ]any
225- if err := json .Unmarshal ([]byte (tempString ), & got ); err != nil {
226- t .Fatalf ("failed to unmarshal result: %v" , err )
227- }
228- if err := json .Unmarshal ([]byte (tc .want ), & want ); err != nil {
229- t .Fatalf ("failed to unmarshal want: %v" , err )
218+ if tc .wantSubstring {
219+ if ! strings .Contains (got , want ) {
220+ t .Fatalf ("unexpected result: got %q, want substring %q" , got , want )
221+ }
222+ return
230223 }
231224
232- if ! reflect . DeepEqual ( got , want ) {
233- t .Fatalf ("unexpected result: got %+v, want %+v " , got , want )
225+ if got != want {
226+ t .Fatalf ("unexpected result: \n got: %s \n want: %s " , got , want )
234227 }
235228 })
236229 }
0 commit comments