Skip to content

Commit

Permalink
refactor(oohelperd): better distinguish different helpers (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
bassosimone authored Aug 17, 2021
1 parent bef5b87 commit ce854e8
Show file tree
Hide file tree
Showing 23 changed files with 48 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package webconnectivity

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package webconnectivity

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package webconnectivity

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal_test
package webconnectivity

import (
"context"
Expand All @@ -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,
Expand All @@ -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,
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package webconnectivity

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package webconnectivity

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package webconnectivity

import (
"encoding/json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal_test
package webconnectivity

import (
"context"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"crypto/tls"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"net/http"
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"context"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"context"
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"github.com/ooni/probe-cli/v3/internal/engine/experiment/websteps"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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,
))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package nwcth
package websteps

import (
"context"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/oohelperd/oohelperd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ce854e8

Please sign in to comment.