168 lines
4.6 KiB
Go
168 lines
4.6 KiB
Go
package execution_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/local/http-client-app/backend/internal/execution"
|
|
"golang.org/x/net/websocket"
|
|
)
|
|
|
|
func TestRunHTTPExecutionSendsRequestAndCapturesResponse(t *testing.T) {
|
|
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
t.Fatalf("expected POST, got %s", r.Method)
|
|
}
|
|
if r.Header.Get("X-Test") != "yes" {
|
|
t.Fatalf("expected X-Test header")
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
_, _ = w.Write([]byte(`{"ok":true}`))
|
|
}))
|
|
defer target.Close()
|
|
|
|
store := execution.NewStore()
|
|
result := store.Create(execution.CreateRequest{
|
|
Type: execution.TypeHTTP,
|
|
Request: &execution.RequestItem{
|
|
Method: "POST",
|
|
URL: target.URL,
|
|
Headers: map[string]string{"X-Test": "yes"},
|
|
Body: `{"name":"Zoe"}`,
|
|
},
|
|
})
|
|
|
|
if result.Status != execution.StatusSucceeded {
|
|
t.Fatalf("expected succeeded, got %s", result.Status)
|
|
}
|
|
|
|
response, ok := result.Result["response"].(execution.ResponseResult)
|
|
if !ok {
|
|
t.Fatalf("expected response result, got %#v", result.Result)
|
|
}
|
|
if response.StatusCode != http.StatusCreated {
|
|
t.Fatalf("expected status 201, got %d", response.StatusCode)
|
|
}
|
|
if response.Body != `{"ok":true}` {
|
|
t.Fatalf("unexpected body: %s", response.Body)
|
|
}
|
|
}
|
|
|
|
func TestRunBatchExecutionCreatesChildren(t *testing.T) {
|
|
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_ = json.NewEncoder(w).Encode(map[string]string{"path": r.URL.Path})
|
|
}))
|
|
defer target.Close()
|
|
|
|
store := execution.NewStore()
|
|
result := store.Create(execution.CreateRequest{
|
|
Type: execution.TypeBatch,
|
|
Requests: []execution.RequestItem{
|
|
{Method: "GET", URL: target.URL + "/one"},
|
|
{Method: "GET", URL: target.URL + "/two"},
|
|
},
|
|
})
|
|
|
|
if result.Status != execution.StatusSucceeded {
|
|
t.Fatalf("expected succeeded, got %s", result.Status)
|
|
}
|
|
if len(result.Children) != 2 {
|
|
t.Fatalf("expected two child executions, got %d", len(result.Children))
|
|
}
|
|
}
|
|
|
|
func TestRunLoadTestReportsTotals(t *testing.T) {
|
|
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
defer target.Close()
|
|
|
|
store := execution.NewStore()
|
|
result := store.Create(execution.CreateRequest{
|
|
Type: execution.TypeLoadTest,
|
|
Request: &execution.RequestItem{
|
|
Method: "GET",
|
|
URL: target.URL,
|
|
},
|
|
Options: map[string]any{
|
|
"requests": float64(4),
|
|
"concurrency": float64(2),
|
|
},
|
|
})
|
|
|
|
if result.Status != execution.StatusSucceeded {
|
|
t.Fatalf("expected succeeded, got %s", result.Status)
|
|
}
|
|
if result.Result["totalRequests"] != 4 {
|
|
t.Fatalf("expected totalRequests=4, got %#v", result.Result)
|
|
}
|
|
if result.Result["succeeded"] != 4 {
|
|
t.Fatalf("expected succeeded=4, got %#v", result.Result)
|
|
}
|
|
}
|
|
|
|
func TestRunWebSocketExecutionDialsAndExchangesMessages(t *testing.T) {
|
|
target := httptest.NewServer(websocket.Handler(func(conn *websocket.Conn) {
|
|
var message string
|
|
if err := websocket.Message.Receive(conn, &message); err != nil {
|
|
t.Errorf("failed to receive websocket message: %v", err)
|
|
return
|
|
}
|
|
if err := websocket.Message.Send(conn, "echo:"+message); err != nil {
|
|
t.Errorf("failed to send websocket message: %v", err)
|
|
}
|
|
}))
|
|
defer target.Close()
|
|
|
|
store := execution.NewStore()
|
|
result := store.Create(execution.CreateRequest{
|
|
Type: execution.TypeWebSocket,
|
|
Request: &execution.RequestItem{
|
|
Method: "GET",
|
|
URL: "ws" + strings.TrimPrefix(target.URL, "http"),
|
|
},
|
|
Options: map[string]any{
|
|
"messages": []any{"hello"},
|
|
},
|
|
})
|
|
|
|
if result.Status != execution.StatusSucceeded {
|
|
t.Fatalf("expected succeeded, got %s: %#v", result.Status, result)
|
|
}
|
|
received, ok := result.Result["received"].([]string)
|
|
if !ok {
|
|
t.Fatalf("expected received websocket messages, got %#v", result.Result)
|
|
}
|
|
if len(received) != 1 || received[0] != "echo:hello" {
|
|
t.Fatalf("expected echo response, got %#v", received)
|
|
}
|
|
}
|
|
|
|
func TestRunWebSocketExecutionFailsWhenEndpointIsNotWebSocket(t *testing.T) {
|
|
target := httptest.NewServer(http.NotFoundHandler())
|
|
defer target.Close()
|
|
|
|
store := execution.NewStore()
|
|
result := store.Create(execution.CreateRequest{
|
|
Type: execution.TypeWebSocket,
|
|
Request: &execution.RequestItem{
|
|
Method: "GET",
|
|
URL: "ws" + strings.TrimPrefix(target.URL, "http"),
|
|
},
|
|
Options: map[string]any{
|
|
"messages": []any{"hello"},
|
|
},
|
|
})
|
|
|
|
if result.Status != execution.StatusFailed {
|
|
t.Fatalf("expected failed websocket execution, got %#v", result)
|
|
}
|
|
if result.Error == "" {
|
|
t.Fatalf("expected websocket failure error")
|
|
}
|
|
}
|