Skip to content

Commit

Permalink
refactor(mlablocate*): use internal testing (ooni#382)
Browse files Browse the repository at this point in the history
This is not an external package and it's fine to just use internal testing.

It reduces the complexity a little bit.
  • Loading branch information
bassosimone authored Jun 15, 2021
1 parent c8dbb2f commit e840dd4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 58 deletions.
17 changes: 8 additions & 9 deletions internal/engine/internal/mlablocate/mlablocate_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mlablocate_test
package mlablocate

import (
"context"
Expand All @@ -9,11 +9,10 @@ import (
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/internal/mlablocate"
)

func TestWithoutProxy(t *testing.T) {
client := mlablocate.NewClient(
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
Expand All @@ -28,7 +27,7 @@ func TestWithoutProxy(t *testing.T) {
}

func Test404Response(t *testing.T) {
client := mlablocate.NewClient(
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
Expand All @@ -43,7 +42,7 @@ func Test404Response(t *testing.T) {
}

func TestNewRequestFailure(t *testing.T) {
client := mlablocate.NewClient(
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
Expand All @@ -59,7 +58,7 @@ func TestNewRequestFailure(t *testing.T) {
}

func TestHTTPClientDoFailure(t *testing.T) {
client := mlablocate.NewClient(
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
Expand All @@ -86,7 +85,7 @@ func (txp *roundTripFails) RoundTrip(*http.Request) (*http.Response, error) {
}

func TestCannotReadBody(t *testing.T) {
client := mlablocate.NewClient(
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
Expand Down Expand Up @@ -128,7 +127,7 @@ func (b *readingBodyFailsBody) Close() error {
}

func TestInvalidJSON(t *testing.T) {
client := mlablocate.NewClient(
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
Expand Down Expand Up @@ -169,7 +168,7 @@ func (b *invalidJSONBody) Close() error {
}

func TestEmptyFQDN(t *testing.T) {
client := mlablocate.NewClient(
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/internal/mlablocatev2/mlablocatev2.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package mlablocatev2 use m-lab locate services API v2.
// Package mlablocatev2 implements m-lab locate services API v2.
package mlablocatev2

import (
Expand Down

This file was deleted.

63 changes: 31 additions & 32 deletions internal/engine/internal/mlablocatev2/mlablocatev2_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mlablocatev2_test
package mlablocatev2

import (
"context"
Expand All @@ -10,14 +10,13 @@ import (
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/internal/mlablocatev2"
)

func TestSuccess(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
result, err := client.QueryNDT7(context.Background())
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -51,9 +50,9 @@ func Test404Response(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
result, err := client.Query(context.Background(), "nonexistent")
if !errors.Is(err, mlablocatev2.ErrRequestFailed) {
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
result, err := client.query(context.Background(), "nonexistent")
if !errors.Is(err, ErrRequestFailed) {
t.Fatal("not the error we expected")
}
if result.Results != nil {
Expand All @@ -62,9 +61,9 @@ func Test404Response(t *testing.T) {
}

func TestNewRequestFailure(t *testing.T) {
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client.Hostname = "\t"
result, err := client.Query(context.Background(), "nonexistent")
result, err := client.query(context.Background(), "nonexistent")
if err == nil || !strings.Contains(err.Error(), "invalid URL escape") {
t.Fatal("not the error we expected")
}
Expand All @@ -74,12 +73,12 @@ func TestNewRequestFailure(t *testing.T) {
}

func TestHTTPClientDoFailure(t *testing.T) {
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
expected := errors.New("mocked error")
client.HTTPClient = &http.Client{
Transport: mlablocatev2.FakeTransport{Err: expected},
Transport: FakeTransport{Err: expected},
}
result, err := client.Query(context.Background(), "nonexistent")
result, err := client.query(context.Background(), "nonexistent")
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
Expand All @@ -89,19 +88,19 @@ func TestHTTPClientDoFailure(t *testing.T) {
}

func TestCannotReadBody(t *testing.T) {
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
expected := errors.New("mocked error")
client.HTTPClient = &http.Client{
Transport: mlablocatev2.FakeTransport{
Transport: FakeTransport{
Resp: &http.Response{
StatusCode: 200,
Body: mlablocatev2.FakeBody{
Body: FakeBody{
Err: expected,
},
},
},
}
result, err := client.Query(context.Background(), "nonexistent")
result, err := client.query(context.Background(), "nonexistent")
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
Expand All @@ -111,19 +110,19 @@ func TestCannotReadBody(t *testing.T) {
}

func TestInvalidJSON(t *testing.T) {
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client.HTTPClient = &http.Client{
Transport: mlablocatev2.FakeTransport{
Transport: FakeTransport{
Resp: &http.Response{
StatusCode: 200,
Body: mlablocatev2.FakeBody{
Body: FakeBody{
Err: io.EOF,
Data: []byte(`{`),
},
},
},
}
result, err := client.Query(context.Background(), "nonexistent")
result, err := client.query(context.Background(), "nonexistent")
if err == nil || !strings.Contains(err.Error(), "unexpected end of JSON input") {
t.Fatal("not the error we expected")
}
Expand All @@ -133,20 +132,20 @@ func TestInvalidJSON(t *testing.T) {
}

func TestEmptyResponse(t *testing.T) {
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client.HTTPClient = &http.Client{
Transport: mlablocatev2.FakeTransport{
Transport: FakeTransport{
Resp: &http.Response{
StatusCode: 200,
Body: mlablocatev2.FakeBody{
Body: FakeBody{
Err: io.EOF,
Data: []byte(`{}`),
},
},
},
}
result, err := client.QueryNDT7(context.Background())
if !errors.Is(err, mlablocatev2.ErrEmptyResponse) {
if !errors.Is(err, ErrEmptyResponse) {
t.Fatal("not the error we expected")
}
if result != nil {
Expand All @@ -155,17 +154,17 @@ func TestEmptyResponse(t *testing.T) {
}

func TestNDT7QueryFails(t *testing.T) {
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client.HTTPClient = &http.Client{
Transport: mlablocatev2.FakeTransport{
Transport: FakeTransport{
Resp: &http.Response{
StatusCode: 404,
Body: mlablocatev2.FakeBody{Err: io.EOF},
Body: FakeBody{Err: io.EOF},
},
},
}
result, err := client.QueryNDT7(context.Background())
if !errors.Is(err, mlablocatev2.ErrRequestFailed) {
if !errors.Is(err, ErrRequestFailed) {
t.Fatal("not the error we expected")
}
if result != nil {
Expand All @@ -174,12 +173,12 @@ func TestNDT7QueryFails(t *testing.T) {
}

func TestNDT7InvalidURLs(t *testing.T) {
client := mlablocatev2.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client := NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
client.HTTPClient = &http.Client{
Transport: mlablocatev2.FakeTransport{
Transport: FakeTransport{
Resp: &http.Response{
StatusCode: 200,
Body: mlablocatev2.FakeBody{
Body: FakeBody{
Data: []byte(
`{"results":[{"machine":"mlab3-mil04.mlab-oti.measurement-lab.org","urls":{"wss:///ndt/v7/download":":","wss:///ndt/v7/upload":":"}}]}`),
Err: io.EOF,
Expand All @@ -188,7 +187,7 @@ func TestNDT7InvalidURLs(t *testing.T) {
},
}
result, err := client.QueryNDT7(context.Background())
if !errors.Is(err, mlablocatev2.ErrEmptyResponse) {
if !errors.Is(err, ErrEmptyResponse) {
t.Fatal("not the error we expected")
}
if result != nil {
Expand Down Expand Up @@ -220,7 +219,7 @@ func TestEntryRecordSite(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
er := mlablocatev2.EntryRecord{
er := entryRecord{
Machine: tt.fields.Machine,
URLs: tt.fields.URLs,
}
Expand Down

0 comments on commit e840dd4

Please sign in to comment.