-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle nil opts and add webtests flag
Signed-off-by: Christian Stewart <[email protected]>
- Loading branch information
Showing
4 changed files
with
159 additions
and
2 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,59 @@ | ||
# Web Tests | ||
|
||
Some tests in this project require internet access to perform HTTP requests to external services. These tests are controlled by the `webtests` build tag. | ||
|
||
## Running Web Tests | ||
|
||
To run tests that include web requests, use the following command: | ||
|
||
``` | ||
go test -tags webtests ./... | ||
``` | ||
|
||
This will enable the `webtests` build tag and run all tests, including those that make HTTP requests to external services. | ||
|
||
### WebAssembly | ||
|
||
This package can be tested in a browser environment using [`wasmbrowsertest`](https://github.com/agnivade/wasmbrowsertest). | ||
|
||
1. Install `wasmbrowsertest`: | ||
```bash | ||
go install github.com/agnivade/wasmbrowsertest@latest | ||
``` | ||
|
||
2. Rename the `wasmbrowsertest` binary to `go_js_wasm_exec`: | ||
```bash | ||
mv $(go env GOPATH)/bin/wasmbrowsertest $(go env GOPATH)/bin/go_js_wasm_exec | ||
``` | ||
|
||
3. Run the tests with the `js` GOOS and `wasm` GOARCH: | ||
```bash | ||
GOOS=js GOARCH=wasm go test -tags "webtests" -v ./... | ||
``` | ||
|
||
This will compile the tests to WebAssembly and run them in a headless browser environment. | ||
|
||
|
||
## Skipping Web Tests | ||
|
||
By default, tests that require internet access are not run. To run all other tests without the web tests, simply run: | ||
|
||
``` | ||
go test ./... | ||
``` | ||
|
||
This will skip any tests that have the `webtests` build tag. | ||
|
||
## Writing Web Tests | ||
|
||
When writing tests that require internet access or make HTTP requests to external services, make sure to add the `webtests` build tag to the test file. For example: | ||
|
||
```go | ||
//go:build js && webtests | ||
|
||
package mypackage | ||
|
||
// Test code here | ||
``` | ||
|
||
This ensures that these tests are only run when explicitly requested with the `webtests` build tag. |
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//go:build js && webtests | ||
|
||
package httplog_fetch | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"testing" | ||
|
||
fetch "github.com/aperturerobotics/bifrost/util/js-fetch" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestFetch(t *testing.T) { | ||
// Create a logger | ||
logger := logrus.New() | ||
logger.SetLevel(logrus.DebugLevel) | ||
|
||
// Test cases | ||
testCases := []struct { | ||
name string | ||
url string | ||
opts *fetch.Opts | ||
verbose bool | ||
expectError bool | ||
}{ | ||
{ | ||
name: "Successful GET request", | ||
url: "https://httpbin.org/get", | ||
verbose: true, | ||
}, | ||
{ | ||
name: "POST request with headers", | ||
url: "https://httpbin.org/post", | ||
opts: &fetch.Opts{ | ||
Method: "POST", | ||
Headers: map[string]string{ | ||
"Content-Type": "application/json", | ||
}, | ||
Body: bytes.NewReader([]byte(`{"test": "data"}`)), | ||
}, | ||
verbose: true, | ||
}, | ||
{ | ||
name: "Non-existent URL", | ||
url: "https://thisurldoesnotexist.example.com", | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
le := logger.WithField("test", tc.name) | ||
|
||
resp, err := Fetch(le, tc.url, tc.opts, tc.verbose) | ||
|
||
if tc.expectError { | ||
if err == nil { | ||
t.Errorf("Expected an error, but got nil") | ||
} | ||
if resp != nil { | ||
t.Errorf("Expected nil response, but got %v", resp) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
if resp == nil { | ||
t.Fatalf("Expected non-nil response, but got nil") | ||
} | ||
if resp.Status != http.StatusOK { | ||
t.Errorf("Expected status %d, but got %d", http.StatusOK, resp.Status) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFetchWithNilLogger(t *testing.T) { | ||
resp, err := Fetch(nil, "https://httpbin.org/get", nil, false) | ||
if err != nil { | ||
t.Fatalf("Unexpected error: %v", err) | ||
} | ||
if resp == nil { | ||
t.Fatal("Expected non-nil response, but got nil") | ||
} | ||
if resp.Status != http.StatusOK { | ||
t.Errorf("Expected status %d, but got %d", http.StatusOK, resp.Status) | ||
} | ||
} |
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