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 ( import (
"context" "context"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package nwcth package websteps
import ( import (
"context" "context"
@ -75,8 +75,9 @@ func TestGenerateDNSFailure(t *testing.T) {
func TestGenerate(t *testing.T) { func TestGenerate(t *testing.T) {
u, err := url.Parse("http://www.google.com") 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") 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{ rts := []*RoundTrip{
{ {
Proto: "http", Proto: "http",
@ -390,8 +391,9 @@ func TestGenerateHTTPDoFails(t *testing.T) {
resolver: newResolver(), resolver: newResolver(),
} }
u, err := url.Parse("http://www.google.com") 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") 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{ rts := []*RoundTrip{
{ {
Proto: "http", Proto: "http",

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package nwcth package websteps
import ( import (
"context" "context"
@ -17,7 +17,7 @@ type (
ControlResponse = websteps.ControlResponse 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 // Config contains the building blocks of the testhelper algorithm
type Config struct { type Config struct {

View File

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

View File

@ -1,4 +1,4 @@
package nwcth package websteps
import ( import (
"github.com/ooni/probe-cli/v3/internal/engine/experiment/websteps" "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 // See https://github.com/ooni/spec/blob/master/backends/th-007-nwcth.md
package nwcth package websteps
import ( import (
"encoding/json" "encoding/json"
@ -30,7 +30,7 @@ type Handler struct {
} }
// ServeHTTP implements http.Handler.ServeHTTP. // 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( w.Header().Add("Server", fmt.Sprintf(
"oohelperd/%s ooniprobe-engine/%s", version.Version, version.Version, "oohelperd/%s ooniprobe-engine/%s", version.Version, version.Version,
)) ))

View File

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

View File

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