refactor(oohelperd): better distinguish different helpers (#434)

Part of https://github.com/ooni/probe/issues/1733
This commit is contained in:
Simone Basso 2021-08-17 11:23:53 +02:00 committed by GitHub
parent bef5b87a8a
commit ce854e8ae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 48 additions and 45 deletions

View File

@ -1,4 +1,4 @@
package internal
package webconnectivity
import (
"context"

View File

@ -1,4 +1,4 @@
package internal
package webconnectivity
import (
"context"

View File

@ -1,4 +1,4 @@
package internal
package webconnectivity
import (
"context"

View File

@ -1,4 +1,4 @@
package internal_test
package webconnectivity
import (
"context"
@ -7,16 +7,14 @@ import (
"strings"
"sync"
"testing"
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal"
)
func TestHTTPDoWithInvalidURL(t *testing.T) {
ctx := context.Background()
wg := new(sync.WaitGroup)
httpch := make(chan internal.CtrlHTTPResponse, 1)
httpch := make(chan CtrlHTTPResponse, 1)
wg.Add(1)
go internal.HTTPDo(ctx, &internal.HTTPConfig{
go HTTPDo(ctx, &HTTPConfig{
Client: http.DefaultClient,
Headers: nil,
MaxAcceptableBody: 1 << 24,
@ -36,11 +34,11 @@ func TestHTTPDoWithHTTPTransportFailure(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
wg := new(sync.WaitGroup)
httpch := make(chan internal.CtrlHTTPResponse, 1)
httpch := make(chan CtrlHTTPResponse, 1)
wg.Add(1)
go internal.HTTPDo(ctx, &internal.HTTPConfig{
go HTTPDo(ctx, &HTTPConfig{
Client: &http.Client{
Transport: internal.FakeTransport{
Transport: FakeTransport{
Err: expected,
},
},

View File

@ -1,4 +1,4 @@
package internal
package webconnectivity
import (
"context"

View File

@ -1,4 +1,4 @@
package internal
package webconnectivity
import (
"context"

View File

@ -1,4 +1,4 @@
package internal
package webconnectivity
import (
"encoding/json"

View File

@ -1,4 +1,4 @@
package internal_test
package webconnectivity
import (
"context"
@ -10,7 +10,6 @@ import (
"strings"
"testing"
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal"
"github.com/ooni/probe-cli/v3/internal/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@ -52,7 +51,7 @@ const requestWithoutDomainName = `{
}`
func TestWorkingAsIntended(t *testing.T) {
handler := internal.Handler{
handler := Handler{
Client: http.DefaultClient,
Dialer: new(net.Dialer),
MaxAcceptableBody: 1 << 24,
@ -144,15 +143,15 @@ func TestWorkingAsIntended(t *testing.T) {
func TestHandlerWithRequestBodyReadingError(t *testing.T) {
expected := errors.New("mocked error")
handler := internal.Handler{MaxAcceptableBody: 1 << 24}
rw := internal.NewFakeResponseWriter()
handler := Handler{MaxAcceptableBody: 1 << 24}
rw := NewFakeResponseWriter()
req := &http.Request{
Method: "POST",
Header: map[string][]string{
"Content-Type": {"application/json"},
"Content-Length": {"2048"},
},
Body: &internal.FakeBody{Err: expected},
Body: &FakeBody{Err: expected},
}
handler.ServeHTTP(rw, req)
if rw.StatusCode != 400 {

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"crypto/tls"

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"net/http"
@ -56,6 +56,7 @@ func TestExploreSuccessWithH3(t *testing.T) {
func TestGetSuccess(t *testing.T) {
u, err := url.Parse("https://example.com")
runtimex.PanicOnError(err, "url.Parse failed for clearly good URL")
resp, err := explorer.get(u, nil)
if err != nil {
t.Fatal("unexpected error")
@ -72,6 +73,7 @@ func TestGetSuccess(t *testing.T) {
func TestGetFailure(t *testing.T) {
u, err := url.Parse("https://example.example")
runtimex.PanicOnError(err, "url.Parse failed for clearly good URL")
resp, err := explorer.get(u, nil)
if err == nil {
t.Fatal("expected an error here")
@ -83,6 +85,7 @@ func TestGetFailure(t *testing.T) {
func TestGetH3Success(t *testing.T) {
u, err := url.Parse("https://www.google.com")
runtimex.PanicOnError(err, "url.Parse failed for clearly good URL")
h3u := &h3URL{URL: u, proto: "h3"}
resp, err := explorer.getH3(h3u, nil)
if err != nil {
@ -100,6 +103,7 @@ func TestGetH3Success(t *testing.T) {
func TestGetH3Failure(t *testing.T) {
u, err := url.Parse("https://www.google.google")
runtimex.PanicOnError(err, "url.Parse failed for clearly good URL")
h3u := &h3URL{URL: u, proto: "h3"}
resp, err := explorer.getH3(h3u, nil)
if err == nil {

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"context"

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"context"

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"context"
@ -75,8 +75,9 @@ func TestGenerateDNSFailure(t *testing.T) {
func TestGenerate(t *testing.T) {
u, err := url.Parse("http://www.google.com")
runtimex.PanicOnError(err, "url.Parse failed for clearly good URL")
u2, err := url.Parse("https://www.google.com")
runtimex.PanicOnError(err, "url.Parse failed")
runtimex.PanicOnError(err, "url.Parse failed for clearly good URL")
rts := []*RoundTrip{
{
Proto: "http",
@ -390,8 +391,9 @@ func TestGenerateHTTPDoFails(t *testing.T) {
resolver: newResolver(),
}
u, err := url.Parse("http://www.google.com")
runtimex.PanicOnError(err, "url.Parse failed for clearly good URL")
u2, err := url.Parse("https://www.google.com")
runtimex.PanicOnError(err, "url.Parse failed")
runtimex.PanicOnError(err, "url.Parse failed for clearly good URL")
rts := []*RoundTrip{
{
Proto: "http",

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"errors"

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"net/http"

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"context"

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"context"
@ -17,7 +17,7 @@ type (
ControlResponse = websteps.ControlResponse
)
var ErrInternalServer = errors.New("Internal server failure")
var ErrInternalServer = errors.New("internal server error")
// Config contains the building blocks of the testhelper algorithm
type Config struct {

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"context"

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"github.com/ooni/probe-cli/v3/internal/engine/experiment/websteps"

View File

@ -1,7 +1,7 @@
// Package nwcth implements the new web connectivity test helper.
// Package websteps implements the websteps test helper.
//
// See https://github.com/ooni/spec/blob/master/backends/th-007-nwcth.md
package nwcth
package websteps
import (
"encoding/json"
@ -30,7 +30,7 @@ type Handler struct {
}
// ServeHTTP implements http.Handler.ServeHTTP.
func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Server", fmt.Sprintf(
"oohelperd/%s ooniprobe-engine/%s", version.Version, version.Version,
))

View File

@ -1,4 +1,4 @@
package nwcth
package websteps
import (
"context"
@ -105,7 +105,7 @@ const requestWithoutDomainName = `{
func TestWorkingAsIntended(t *testing.T) {
handler := Handler{Config: &Config{}}
srv := httptest.NewServer(handler)
srv := httptest.NewServer(&handler)
defer srv.Close()
type expectationSpec struct {
name string
@ -205,7 +205,7 @@ func TestWorkingAsIntended(t *testing.T) {
func TestHandlerWithInternalServerError(t *testing.T) {
handler := Handler{Config: &Config{explorer: &MockExplorer{}}}
srv := httptest.NewServer(handler)
srv := httptest.NewServer(&handler)
defer srv.Close()
body := strings.NewReader(`{"url": "https://example.com"}`)
req, err := http.NewRequest("POST", srv.URL, body)

View File

@ -9,8 +9,8 @@ import (
"time"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal"
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal/nwcth"
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal/webconnectivity"
"github.com/ooni/probe-cli/v3/internal/cmd/oohelperd/internal/websteps"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
)
@ -53,8 +53,8 @@ func main() {
func testableMain() {
mux := http.NewServeMux()
mux.Handle("/api/unstable/nwcth", nwcth.Handler{Config: &nwcth.Config{}})
mux.Handle("/", internal.Handler{
mux.Handle("/api/unstable/websteps", &websteps.Handler{Config: &websteps.Config{}})
mux.Handle("/", webconnectivity.Handler{
Client: httpx,
Dialer: dialer,
MaxAcceptableBody: maxAcceptableBody,