refactor(netxlite): improve tests for http and http3 (#487)
* refactor(netxlite): improve tests for http and http3 See https://github.com/ooni/probe/issues/1591 * Update internal/netxlite/http3.go
This commit is contained in:
parent
6d39118b26
commit
493b72b170
6 changed files with 547 additions and 344 deletions
19
internal/netxlite/mocks/http3.go
Normal file
19
internal/netxlite/mocks/http3.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package mocks
|
||||
|
||||
import "net/http"
|
||||
|
||||
// HTTP3RoundTripper allows mocking http3.RoundTripper.
|
||||
type HTTP3RoundTripper struct {
|
||||
MockRoundTrip func(req *http.Request) (*http.Response, error)
|
||||
MockClose func() error
|
||||
}
|
||||
|
||||
// RoundTrip calls MockRoundTrip.
|
||||
func (txp *HTTP3RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return txp.MockRoundTrip(req)
|
||||
}
|
||||
|
||||
// Close calls MockClose.
|
||||
func (txp *HTTP3RoundTripper) Close() error {
|
||||
return txp.MockClose()
|
||||
}
|
||||
37
internal/netxlite/mocks/http3_test.go
Normal file
37
internal/netxlite/mocks/http3_test.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package mocks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHTTP3RoundTripper(t *testing.T) {
|
||||
t.Run("RoundTrip", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
txp := &HTTP3RoundTripper{
|
||||
MockRoundTrip: func(req *http.Request) (*http.Response, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
resp, err := txp.RoundTrip(&http.Request{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if resp != nil {
|
||||
t.Fatal("unexpected resp")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Close", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
txp := &HTTP3RoundTripper{
|
||||
MockClose: func() error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
if err := txp.Close(); !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue