refactor(mlablocate*): move from i/e/internal to internal (#385)

We've been flattening the package structure for some time now.

While there, add very basic examples.
This commit is contained in:
Simone Basso
2021-06-15 19:51:03 +02:00
committed by GitHub
parent d84cf5b69f
commit 85b16c8bd2
9 changed files with 40 additions and 2 deletions
+19
View File
@@ -0,0 +1,19 @@
package mlablocate_test
import (
"context"
"fmt"
"net/http"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/mlablocate"
)
func Example_usage() {
clnt := mlablocate.NewClient(http.DefaultClient, log.Log, "miniooni/0.1.0-dev")
result, err := clnt.Query(context.Background(), "neubot/dash")
if err != nil {
log.WithError(err).Fatal("clnt.Query failed")
}
fmt.Printf("%s\n", result.FQDN)
}
+109
View File
@@ -0,0 +1,109 @@
// Package mlablocate contains a locate.measurementlab.net client
// implementing v1 of the locate API. This version of the API isn't
// suitable for requesting servers for ndt7. You should use the
// mlablocatev2 package for that.
package mlablocate
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/ooni/probe-cli/v3/internal/iox"
)
// Logger is the logger expected by this package.
type Logger interface {
// Debugf formats and emits a debug message.
Debugf(format string, v ...interface{})
}
// HTTPClient is anything that looks like an http.Client.
type HTTPClient interface {
// Do behaves like http.Client.Do.
Do(req *http.Request) (*http.Response, error)
}
// Client is a locate.measurementlab.net client. Please use the
// NewClient factory to construct a new instance of client, otherwise
// you MUST fill all the fields marked as MANDATORY.
type Client struct {
// HTTPClient is the MANDATORY http client to use.
HTTPClient HTTPClient
// Hostname is the MANDATORY hostname of the mlablocate API.
Hostname string
// Logger is the MANDATORY logger to use.
Logger Logger
// Scheme is the MANDATORY scheme to use (http or https).
Scheme string
// UserAgent is the MANDATORY user-agent to use.
UserAgent string
}
// NewClient creates a new locate.measurementlab.net client.
func NewClient(httpClient HTTPClient, logger Logger, userAgent string) *Client {
return &Client{
HTTPClient: httpClient,
Hostname: "locate.measurementlab.net",
Logger: logger,
Scheme: "https",
UserAgent: userAgent,
}
}
// Result is a result of a query to locate.measurementlab.net.
type Result struct {
// FQDN is the mlab server's FQDN.
FQDN string `json:"fqdn"`
// Site is the ID of the site where the server is.
Site string `json:"site"`
}
// Query performs a locate.measurementlab.net query. This function returns
// either valid result, on success, or an error, on failure.
// (Note thay you cannot use this API to query for ndt7 servers. You should
// use the mlablocatev2 API to obtain such servers.)
func (c *Client) Query(ctx context.Context, tool string) (Result, error) {
// TODO(bassosimone): this code should probably be
// refactored to use the httpx package.
URL := &url.URL{
Scheme: c.Scheme,
Host: c.Hostname,
Path: tool,
}
req, err := http.NewRequestWithContext(ctx, "GET", URL.String(), nil)
if err != nil {
return Result{}, err
}
req.Header.Add("User-Agent", c.UserAgent)
c.Logger.Debugf("mlablocate: GET %s", URL.String())
resp, err := c.HTTPClient.Do(req)
if err != nil {
return Result{}, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return Result{}, fmt.Errorf("mlablocate: non-200 status code: %d", resp.StatusCode)
}
data, err := iox.ReadAllContext(ctx, resp.Body)
if err != nil {
return Result{}, err
}
c.Logger.Debugf("mlablocate: %s", string(data))
var result Result
if err := json.Unmarshal(data, &result); err != nil {
return Result{}, err
}
if result.FQDN == "" {
return Result{}, errors.New("mlablocate: returned empty FQDN")
}
return result, nil
}
+205
View File
@@ -0,0 +1,205 @@
package mlablocate
import (
"context"
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/apex/log"
)
func TestSuccess(t *testing.T) {
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
)
result, err := client.Query(context.Background(), "neubot/dash")
if err != nil {
t.Fatal(err)
}
if result.FQDN == "" {
t.Fatal("unexpected empty fqdn")
}
}
func Test404Response(t *testing.T) {
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
)
result, err := client.Query(context.Background(), "nonexistent")
if err == nil || !strings.Contains(err.Error(), "mlablocate: non-200 status code") {
t.Fatal("not the error we expected")
}
if result.FQDN != "" {
t.Fatal("expected empty fqdn")
}
}
func TestNewRequestFailure(t *testing.T) {
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
)
client.Hostname = "\t"
result, err := client.Query(context.Background(), "nonexistent")
if err == nil || !strings.Contains(err.Error(), "invalid URL escape") {
t.Fatal("not the error we expected")
}
if result.FQDN != "" {
t.Fatal("expected empty fqdn")
}
}
func TestHTTPClientDoFailure(t *testing.T) {
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
)
expected := errors.New("mocked error")
client.HTTPClient = &http.Client{
Transport: &roundTripFails{Error: expected},
}
result, err := client.Query(context.Background(), "nonexistent")
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if result.FQDN != "" {
t.Fatal("expected empty fqdn")
}
}
type roundTripFails struct {
Error error
}
func (txp *roundTripFails) RoundTrip(*http.Request) (*http.Response, error) {
return nil, txp.Error
}
func TestCannotReadBody(t *testing.T) {
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
)
expected := errors.New("mocked error")
client.HTTPClient = &http.Client{
Transport: &readingBodyFails{Error: expected},
}
result, err := client.Query(context.Background(), "nonexistent")
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if result.FQDN != "" {
t.Fatal("expected empty fqdn")
}
}
type readingBodyFails struct {
Error error
}
func (txp *readingBodyFails) RoundTrip(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: &readingBodyFailsBody{Error: txp.Error},
}, nil
}
type readingBodyFailsBody struct {
Error error
}
func (b *readingBodyFailsBody) Read(p []byte) (int, error) {
return 0, b.Error
}
func (b *readingBodyFailsBody) Close() error {
return nil
}
func TestInvalidJSON(t *testing.T) {
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
)
client.HTTPClient = &http.Client{
Transport: &invalidJSON{},
}
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")
}
if result.FQDN != "" {
t.Fatal("expected empty fqdn")
}
}
type invalidJSON struct{}
func (txp *invalidJSON) RoundTrip(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: &invalidJSONBody{},
}, nil
}
type invalidJSONBody struct{}
func (b *invalidJSONBody) Read(p []byte) (int, error) {
if len(p) < 1 {
return 0, errors.New("slice too short")
}
p[0] = '{'
return 1, io.EOF
}
func (b *invalidJSONBody) Close() error {
return nil
}
func TestEmptyFQDN(t *testing.T) {
client := NewClient(
http.DefaultClient,
log.Log,
"miniooni/0.1.0-dev",
)
client.HTTPClient = &http.Client{
Transport: &emptyFQDN{},
}
result, err := client.Query(context.Background(), "nonexistent")
if err == nil || !strings.HasSuffix(err.Error(), "returned empty FQDN") {
t.Fatal("not the error we expected")
}
if result.FQDN != "" {
t.Fatal("expected empty fqdn")
}
}
type emptyFQDN struct{}
func (txp *emptyFQDN) RoundTrip(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: &emptyFQDNBody{},
}, nil
}
type emptyFQDNBody struct{}
func (b *emptyFQDNBody) Read(p []byte) (int, error) {
return copy(p, []byte(`{"fqdn":""}`)), io.EOF
}
func (b *emptyFQDNBody) Close() error {
return nil
}