mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 18:43:10 -06:00
Add test for pushgateway client
This commit is contained in:
parent
0b90cfb745
commit
ae13d18383
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/internal"
|
||||||
|
|
||||||
"github.com/opentracing/opentracing-go"
|
"github.com/opentracing/opentracing-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -50,8 +52,7 @@ func (h *httpClient) Notify(ctx context.Context, url string, req *NotifyRequest,
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:errcheck
|
defer internal.CloseAndLogIfError(ctx, hresp.Body, "failed to close response body")
|
||||||
defer hresp.Body.Close()
|
|
||||||
|
|
||||||
if hresp.StatusCode == http.StatusOK {
|
if hresp.StatusCode == http.StatusOK {
|
||||||
return json.NewDecoder(hresp.Body).Decode(resp)
|
return json.NewDecoder(hresp.Body).Decode(resp)
|
||||||
|
|
|
||||||
54
internal/pushgateway/client_test.go
Normal file
54
internal/pushgateway/client_test.go
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
package pushgateway
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNotify(t *testing.T) {
|
||||||
|
wantResponse := NotifyResponse{
|
||||||
|
Rejected: []string{"testing"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var i = 0
|
||||||
|
|
||||||
|
svr := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// /notify only accepts POST requests
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
w.WriteHeader(http.StatusNotImplemented)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if i != 0 { // error path
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// happy path
|
||||||
|
json.NewEncoder(w).Encode(wantResponse)
|
||||||
|
}))
|
||||||
|
defer svr.Close()
|
||||||
|
|
||||||
|
cl := NewHTTPClient(true)
|
||||||
|
gotResponse := NotifyResponse{}
|
||||||
|
|
||||||
|
// Test happy path
|
||||||
|
err := cl.Notify(context.Background(), svr.URL, &NotifyRequest{}, &gotResponse)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to notify client")
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(gotResponse, wantResponse) {
|
||||||
|
t.Errorf("expected response %+v, got %+v", wantResponse, gotResponse)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test error path
|
||||||
|
i++
|
||||||
|
err = cl.Notify(context.Background(), svr.URL, &NotifyRequest{}, &gotResponse)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected notifying the pushgateway to fail, but it succeeded")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue