forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(netemx): implement subset of the OONI API (ooni#1203)
This diff implements the subset of the OONI API required for testing Web Connectivity LTE using netemx. Diff adapted from: ooni#1185. Reference issue: ooni/probe#2461. --------- Co-authored-by: kelmenhorst <k.elmenhorst@mailbox.org>
- Loading branch information
1 parent
e41ba5c
commit 7612267
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package netemx | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/ooni/probe-cli/v3/internal/model" | ||
"github.com/ooni/probe-cli/v3/internal/runtimex" | ||
) | ||
|
||
// OOAPIHandler is an [http.Handler] implementing the OONI API. | ||
type OOAPIHandler struct{} | ||
|
||
var _ http.Handler = &OOAPIHandler{} | ||
|
||
// ServeHTTP implements [http.Handler]. | ||
func (p *OOAPIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
switch { | ||
case r.URL.Path == "/api/v1/test-helpers" && r.Method == http.MethodGet: | ||
p.getApiV1TestHelpers(w, r) | ||
|
||
default: | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
} | ||
|
||
func (p *OOAPIHandler) getApiV1TestHelpers(w http.ResponseWriter, r *http.Request) { | ||
resp := map[string][]model.OOAPIService{ | ||
"web-connectivity": { | ||
{ | ||
Address: "https://2.th.ooni.org", | ||
Type: "https", | ||
}, | ||
{ | ||
Address: "https://3.th.ooni.org", | ||
Type: "https", | ||
}, | ||
{ | ||
Address: "https://0.th.ooni.org", | ||
Type: "https", | ||
}, | ||
{ | ||
Address: "https://1.th.ooni.org", | ||
Type: "https", | ||
}, | ||
}, | ||
} | ||
w.Header().Add("Content-Type", "application/json") | ||
w.Write(runtimex.Try1(json.Marshal(resp))) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package netemx | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/ooni/probe-cli/v3/internal/runtimex" | ||
) | ||
|
||
func TestOOAPIHandler(t *testing.T) { | ||
handler := &OOAPIHandler{} | ||
server := httptest.NewServer(handler) | ||
defer server.Close() | ||
|
||
t.Run("for /api/v1/test-helpers with method GET", func(t *testing.T) { | ||
URL := runtimex.Try1(url.Parse(server.URL)) | ||
URL.Path = "/api/v1/test-helpers" | ||
resp, err := http.Get(URL.String()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
t.Fatal("unexpected status code", resp.StatusCode) | ||
} | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expectedBody := `{"web-connectivity":[{"address":"https://2.th.ooni.org","type":"https"},{"address":"https://3.th.ooni.org","type":"https"},{"address":"https://0.th.ooni.org","type":"https"},{"address":"https://1.th.ooni.org","type":"https"}]}` | ||
|
||
t.Log(string(body)) | ||
if diff := cmp.Diff([]byte(expectedBody), body); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
}) | ||
|
||
t.Run("for unsupported URL path", func(t *testing.T) { | ||
URL := runtimex.Try1(url.Parse(server.URL)) | ||
URL.Path = "/antani" | ||
resp, err := http.Get(URL.String()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusNotFound { | ||
t.Fatal("unexpected status code", resp.StatusCode) | ||
} | ||
}) | ||
} |
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