2022-07-05 19:10:39 +02:00
|
|
|
package main
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
2021-06-15 11:57:40 +02:00
|
|
|
"context"
|
2021-02-02 12:05:47 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-07-05 20:25:18 +02:00
|
|
|
"io"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-08-28 22:26:58 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
2022-06-05 18:44:17 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2022-07-05 20:25:18 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
refactor: start pivoting netx (#396)
What do I mean by pivoting? Netx is currently organized by row:
```
| dialer | quicdialer | resolver | ...
saving | | | | ...
errorwrapping | | | | ...
logging | | | | ...
mocking/sys | | | | ...
```
Every row needs to implement saving, errorwrapping, logging, mocking (or
adapting to the system or to some underlying library).
This causes cross package dependencies and, in turn, complexity. For
example, we need the `trace` package for supporting saving.
And `dialer`, `quickdialer`, et al. need to depend on such a package.
The same goes for errorwrapping.
This arrangement further complicates testing. For example, I am
currently working on https://github.com/ooni/probe/issues/1505 and
I realize it need to repeat integration tests in multiple places.
Let's say instead we pivot the above matrix as follows:
```
| saving | errorwrapping | logging | ...
dialer | | | | ...
quicdialer | | | | ...
logging | | | | ...
mocking/sys | | | | ...
...
```
In this way, now every row contains everything related to a specific
action to perform. We can now share code without relying on extra
support packages. What's more, we can write tests and, judding from
the way in which things are made, it seems we only need integration
testing in `errorwrapping` because it's where data quality matters
whereas, in all other cases, unit testing is fine.
I am going, therefore, to proceed with these changes and "pivot"
`netx`. Hopefully, it won't be too painful.
2021-06-23 15:53:12 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const simplerequest = `{
|
|
|
|
"http_request": "https://dns.google",
|
|
|
|
"http_request_headers": {
|
|
|
|
"Accept": [
|
|
|
|
"*/*"
|
|
|
|
],
|
|
|
|
"Accept-Language": [
|
|
|
|
"en-US;q=0.8,en;q=0.5"
|
|
|
|
],
|
|
|
|
"User-Agent": [
|
|
|
|
"Mozilla/5.0"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"tcp_connect": [
|
|
|
|
"8.8.8.8:443"
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
|
|
|
const requestWithoutDomainName = `{
|
|
|
|
"http_request": "https://8.8.8.8",
|
|
|
|
"http_request_headers": {
|
|
|
|
"Accept": [
|
|
|
|
"*/*"
|
|
|
|
],
|
|
|
|
"Accept-Language": [
|
|
|
|
"en-US;q=0.8,en;q=0.5"
|
|
|
|
],
|
|
|
|
"User-Agent": [
|
|
|
|
"Mozilla/5.0"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"tcp_connect": [
|
|
|
|
"8.8.8.8:443"
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
2022-08-28 13:49:24 +02:00
|
|
|
func TestHandlerWorkingAsIntended(t *testing.T) {
|
2022-07-05 19:10:39 +02:00
|
|
|
handler := &handler{
|
2022-08-28 22:26:58 +02:00
|
|
|
BaseLogger: model.DiscardLogger,
|
|
|
|
Indexer: &atomicx.Int64{},
|
2021-02-02 12:05:47 +01:00
|
|
|
MaxAcceptableBody: 1 << 24,
|
2022-08-28 22:26:58 +02:00
|
|
|
NewClient: func(model.Logger) model.HTTPClient {
|
2022-07-05 18:41:35 +02:00
|
|
|
return http.DefaultClient
|
|
|
|
},
|
2022-08-28 22:26:58 +02:00
|
|
|
NewDialer: func(model.Logger) model.Dialer {
|
2022-07-05 18:41:35 +02:00
|
|
|
return netxlite.NewDialerWithStdlibResolver(model.DiscardLogger)
|
|
|
|
},
|
2022-08-28 22:26:58 +02:00
|
|
|
NewResolver: func(model.Logger) model.Resolver {
|
2022-07-05 18:41:35 +02:00
|
|
|
return netxlite.NewUnwrappedStdlibResolver()
|
|
|
|
},
|
2022-08-28 22:26:58 +02:00
|
|
|
NewTLSHandshaker: func(model.Logger) model.TLSHandshaker {
|
2022-08-28 14:34:40 +02:00
|
|
|
return netxlite.NewTLSHandshakerStdlib(model.DiscardLogger)
|
|
|
|
},
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
srv := httptest.NewServer(handler)
|
|
|
|
defer srv.Close()
|
|
|
|
type expectationSpec struct {
|
|
|
|
name string
|
|
|
|
reqMethod string
|
|
|
|
reqContentType string
|
|
|
|
reqBody string
|
|
|
|
respStatusCode int
|
|
|
|
respContentType string
|
|
|
|
parseBody bool
|
|
|
|
}
|
|
|
|
expectations := []expectationSpec{{
|
2022-08-28 14:34:40 +02:00
|
|
|
name: "check for invalid method",
|
|
|
|
reqMethod: "GET",
|
|
|
|
reqContentType: "",
|
|
|
|
reqBody: "",
|
|
|
|
respStatusCode: 400,
|
|
|
|
respContentType: "",
|
|
|
|
parseBody: false,
|
2021-02-02 12:05:47 +01:00
|
|
|
}, {
|
2022-08-28 14:34:40 +02:00
|
|
|
name: "check for invalid content-type",
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqContentType: "",
|
|
|
|
reqBody: "",
|
|
|
|
respStatusCode: 400,
|
|
|
|
respContentType: "",
|
|
|
|
parseBody: false,
|
2021-02-02 12:05:47 +01:00
|
|
|
}, {
|
2022-08-28 14:34:40 +02:00
|
|
|
name: "check for invalid request body",
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqContentType: "application/json",
|
|
|
|
reqBody: "{",
|
|
|
|
respStatusCode: 400,
|
|
|
|
respContentType: "",
|
|
|
|
parseBody: false,
|
2021-02-02 12:05:47 +01:00
|
|
|
}, {
|
2022-08-28 14:34:40 +02:00
|
|
|
name: "with measurement failure",
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqContentType: "application/json",
|
|
|
|
reqBody: `{"http_request": "http://[::1]aaaa"}`,
|
|
|
|
respStatusCode: 400,
|
|
|
|
respContentType: "",
|
|
|
|
parseBody: false,
|
2021-02-02 12:05:47 +01:00
|
|
|
}, {
|
|
|
|
name: "with reasonably good request",
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqContentType: "application/json",
|
|
|
|
reqBody: simplerequest,
|
|
|
|
respStatusCode: 200,
|
|
|
|
respContentType: "application/json",
|
|
|
|
parseBody: true,
|
|
|
|
}, {
|
|
|
|
name: "when there's no domain name in the request",
|
|
|
|
reqMethod: "POST",
|
|
|
|
reqContentType: "application/json",
|
|
|
|
reqBody: requestWithoutDomainName,
|
|
|
|
respStatusCode: 200,
|
|
|
|
respContentType: "application/json",
|
|
|
|
parseBody: true,
|
|
|
|
}}
|
|
|
|
for _, expect := range expectations {
|
|
|
|
t.Run(expect.name, func(t *testing.T) {
|
|
|
|
body := strings.NewReader(expect.reqBody)
|
|
|
|
req, err := http.NewRequest(expect.reqMethod, srv.URL, body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: %+v", expect.name, err)
|
|
|
|
}
|
|
|
|
if expect.reqContentType != "" {
|
|
|
|
req.Header.Add("content-type", expect.reqContentType)
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: %+v", expect.name, err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != expect.respStatusCode {
|
|
|
|
t.Fatalf("unexpected status code: %+v", resp.StatusCode)
|
|
|
|
}
|
|
|
|
if v := resp.Header.Get("content-type"); v != expect.respContentType {
|
|
|
|
t.Fatalf("unexpected content-type: %s", v)
|
|
|
|
}
|
2021-09-28 12:42:01 +02:00
|
|
|
data, err := netxlite.ReadAllContext(context.Background(), resp.Body)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !expect.parseBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var v interface{}
|
|
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandlerWithRequestBodyReadingError(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
2022-07-05 19:10:39 +02:00
|
|
|
handler := handler{MaxAcceptableBody: 1 << 24}
|
2022-07-05 20:25:18 +02:00
|
|
|
var statusCode int
|
|
|
|
headers := http.Header{}
|
|
|
|
rw := &mocks.HTTPResponseWriter{
|
|
|
|
MockWriteHeader: func(code int) {
|
|
|
|
statusCode = code
|
|
|
|
},
|
|
|
|
MockHeader: func() http.Header {
|
|
|
|
return headers
|
|
|
|
},
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
req := &http.Request{
|
|
|
|
Method: "POST",
|
|
|
|
Header: map[string][]string{
|
|
|
|
"Content-Type": {"application/json"},
|
|
|
|
"Content-Length": {"2048"},
|
|
|
|
},
|
2022-07-05 20:25:18 +02:00
|
|
|
Body: io.NopCloser(&mocks.Reader{
|
|
|
|
MockRead: func(b []byte) (int, error) {
|
|
|
|
return 0, expected
|
|
|
|
},
|
|
|
|
}),
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
handler.ServeHTTP(rw, req)
|
2022-07-05 20:25:18 +02:00
|
|
|
if statusCode != 400 {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected status code")
|
|
|
|
}
|
|
|
|
}
|