Skip to content

Commit 804afc6

Browse files
committed
Add test for bad status code
1 parent 9c800ba commit 804afc6

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

client_test.go

+47-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ package xmlrpc
44

55
import (
66
"context"
7+
"io"
8+
"net/http"
9+
"net/http/httptest"
710
"runtime"
811
"sync"
912
"testing"
@@ -131,11 +134,54 @@ func Test_CloseMemoryLeak(t *testing.T) {
131134
}
132135
}
133136

137+
func Test_BadStatus(t *testing.T) {
138+
139+
// this is a mock xmlrpc server which sends an invalid status code on the first request
140+
// and an empty methodResponse for all subsequence requests
141+
first := true
142+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
143+
if first {
144+
first = false
145+
http.Error(w, "bad status", http.StatusInternalServerError)
146+
} else {
147+
io.WriteString(w, `
148+
<?xml version="1.0" encoding="UTF-8"?>
149+
<methodResponse>
150+
<params>
151+
<param>
152+
<value>
153+
<struct></struct>
154+
</value>
155+
</param>
156+
</params>
157+
</methodResponse>
158+
`)
159+
}
160+
}))
161+
162+
client, err := NewClient(ts.URL, nil)
163+
if err != nil {
164+
t.Fatalf("Can't create client: %v", err)
165+
}
166+
defer client.Close()
167+
168+
var result interface{}
169+
170+
// expect an error due to the bad status code
171+
if err := client.Call("method", nil, &result); err == nil {
172+
t.Fatalf("Bad status didn't result in error")
173+
}
174+
175+
// expect subsequent calls to succeed
176+
if err := client.Call("method", nil, &result); err != nil {
177+
t.Fatalf("Failed to recover after bad status: %v", err)
178+
}
179+
}
180+
134181
func newClient(t *testing.T) *Client {
135182
client, err := NewClient("http://localhost:5001", nil)
136183
if err != nil {
137184
t.Fatalf("Can't create client: %v", err)
138185
}
139-
140186
return client
141187
}

0 commit comments

Comments
 (0)