refactor(oohelperd): improve tests implementation (#835)

After this diff has landed, we have addressed all the points
originally published at https://github.com/ooni/probe/issues/2134.
This commit is contained in:
Simone Basso 2022-07-05 20:25:18 +02:00 committed by GitHub
commit d419ed8ac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 186 additions and 168 deletions

View file

@ -4,12 +4,14 @@ import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@ -149,17 +151,30 @@ func TestWorkingAsIntended(t *testing.T) {
func TestHandlerWithRequestBodyReadingError(t *testing.T) {
expected := errors.New("mocked error")
handler := handler{MaxAcceptableBody: 1 << 24}
rw := NewFakeResponseWriter()
var statusCode int
headers := http.Header{}
rw := &mocks.HTTPResponseWriter{
MockWriteHeader: func(code int) {
statusCode = code
},
MockHeader: func() http.Header {
return headers
},
}
req := &http.Request{
Method: "POST",
Header: map[string][]string{
"Content-Type": {"application/json"},
"Content-Length": {"2048"},
},
Body: &FakeBody{Err: expected},
Body: io.NopCloser(&mocks.Reader{
MockRead: func(b []byte) (int, error) {
return 0, expected
},
}),
}
handler.ServeHTTP(rw, req)
if rw.StatusCode != 400 {
if statusCode != 400 {
t.Fatal("unexpected status code")
}
}