refactor(oohelperd): improve tests implementation (#835)
After this diff has landed, we have addressed all the points originally published at https://github.com/ooni/probe/issues/2134.
This commit is contained in:
parent
535a5d3e00
commit
d419ed8ac8
7 changed files with 186 additions and 168 deletions
|
|
@ -1,15 +1,82 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
func TestSmoke(t *testing.T) {
|
||||
// Just check whether we can start and then tear down the server, so
|
||||
// we have coverage of this code and when we see that some lines aren't
|
||||
// covered we know these are genuine places where we're not testing
|
||||
// the code rather than just places like this simple main.
|
||||
go testableMain()
|
||||
srvcancel() // kills the listener
|
||||
srvwg.Wait() // joined
|
||||
func TestWorkAsIntended(t *testing.T) {
|
||||
// let the kernel pick a random free port
|
||||
*endpoint = "127.0.0.1:0"
|
||||
|
||||
// run the main function in a background goroutine
|
||||
go main()
|
||||
|
||||
// prepare the HTTP request body
|
||||
jsonReq := ctrlRequest{
|
||||
HTTPRequest: "https://dns.google",
|
||||
HTTPRequestHeaders: map[string][]string{
|
||||
"Accept": {model.HTTPHeaderAccept},
|
||||
"Accept-Language": {model.HTTPHeaderAcceptLanguage},
|
||||
"User-Agent": {model.HTTPHeaderUserAgent},
|
||||
},
|
||||
TCPConnect: []string{
|
||||
"8.8.8.8:443",
|
||||
"8.8.4.4:443",
|
||||
},
|
||||
}
|
||||
data, err := json.Marshal(jsonReq)
|
||||
runtimex.PanicOnError(err, "cannot marshal request")
|
||||
|
||||
// construct the test helper's URL
|
||||
endpoint := <-srvAddr
|
||||
URL := &url.URL{
|
||||
Scheme: "http",
|
||||
Host: endpoint,
|
||||
Path: "/",
|
||||
}
|
||||
req, err := http.NewRequest("POST", URL.String(), bytes.NewReader(data))
|
||||
runtimex.PanicOnError(err, "cannot create new HTTP request")
|
||||
|
||||
// issue the request and get the response
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatal("unexpected status code", resp.StatusCode)
|
||||
}
|
||||
|
||||
// read the response body
|
||||
data, err = netxlite.ReadAllContext(context.Background(), resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// parse the response
|
||||
var jsonResp ctrlResponse
|
||||
if err := json.Unmarshal(data, &jsonResp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// very simple correctness check
|
||||
if !strings.Contains(jsonResp.HTTPRequest.Title, "Google") {
|
||||
t.Fatal("expected the response title to contain the string Google")
|
||||
}
|
||||
|
||||
// tear down the TH
|
||||
srvCancel()
|
||||
|
||||
// wait for the background goroutine to join
|
||||
srvWg.Wait()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue