bi test
This commit is contained in:
88
builtin_test.go
Normal file
88
builtin_test.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFetch(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("X-Custom-Header", "test-value")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"status":"ok","message":"Hello from httptest"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
result, err := Fetch(server.URL, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, result.OK)
|
||||
assert.Equal(t, http.StatusOK, result.Status)
|
||||
assert.Contains(t, result.Body, "Hello from httptest")
|
||||
assert.Contains(t, result.Body, `"status":"ok"`)
|
||||
assert.Equal(t, "application/json", result.Headers["Content-Type"])
|
||||
assert.Equal(t, "test-value", result.Headers["X-Custom-Header"])
|
||||
}
|
||||
|
||||
func TestFetchHTTPBin(t *testing.T) {
|
||||
result, err := Fetch("https://httpbin.org/get", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.True(t, result.OK)
|
||||
assert.Equal(t, http.StatusOK, result.Status)
|
||||
assert.Contains(t, result.Body, `"args"`)
|
||||
assert.Equal(t, "application/json", result.Headers["Content-Type"])
|
||||
}
|
||||
|
||||
func TestFetchWith404(t *testing.T) {
|
||||
result, err := Fetch("https://httpbin.org/status/404", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.False(t, result.OK)
|
||||
assert.Equal(t, http.StatusNotFound, result.Status)
|
||||
}
|
||||
|
||||
func TestFetchWithInvalidURL(t *testing.T) {
|
||||
_, err := Fetch("http://this-domain-does-not-exist-12345.com", nil)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "failed to fetch")
|
||||
}
|
||||
|
||||
func TestFetchBuiltinIntegration(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.Header().Set("X-Custom", "custom-value")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte("Hello, World!"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
var stdout, stderr strings.Builder
|
||||
tsContent := `
|
||||
const response = fetch("${URL}");
|
||||
console.log("OK:", response.ok);
|
||||
console.log("Status:", response.status);
|
||||
console.log("Body:", response.text());
|
||||
|
||||
console.log("Content-Type:", response.headers.get("content-type") || "undefined");
|
||||
console.log("Content-Type (case sensitive):", response.headers.get("Content-Type") || "undefined");
|
||||
console.log("X-Custom:", response.headers.get("x-custom") || "undefined");
|
||||
console.log("X-Custom (case sensitive):", response.headers.get("X-Custom") || "undefined");
|
||||
`
|
||||
tsContent = strings.Replace(tsContent, "${URL}", server.URL, 1)
|
||||
|
||||
err := executeTypeScriptContent(tsContent, &stdout, &stderr)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, stderr.String(), "Expected no error output")
|
||||
|
||||
output := stdout.String()
|
||||
assert.Contains(t, output, "OK: true")
|
||||
assert.Contains(t, output, "Status: 200")
|
||||
assert.Contains(t, output, "Body: Hello, World!")
|
||||
}
|
||||
Reference in New Issue
Block a user