-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(oohelperd): improve tests implementation (#835)
After this diff has landed, we have addressed all the points originally published at ooni/probe#2134.
- Loading branch information
1 parent
535a5d3
commit d419ed8
Showing
7 changed files
with
186 additions
and
168 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.