Skip to content

Commit

Permalink
Refactor page.on('response') test
Browse files Browse the repository at this point in the history
The refactor tries to work with the dynamic server values. It also
works with the defined types in common instead of a map.
  • Loading branch information
ankur22 committed Jan 30, 2025
1 parent da0ad1a commit fea5839
Showing 1 changed file with 48 additions and 64 deletions.
112 changes: 48 additions & 64 deletions internal/js/modules/k6/browser/tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"image/png"
"io"
"net"
"net/http"
"runtime"
"strconv"
Expand Down Expand Up @@ -2455,23 +2456,23 @@ type request struct {
}

type response struct {
AllHeaders map[string]string `json:"allHeaders"`
Body string `json:"body"`
FrameURL string `json:"frameUrl"`
AcceptLanguageHeader string `json:"acceptLanguageHeader"`
AcceptLanguageHeaders []string `json:"acceptLanguageHeaders"`
Headers map[string]string `json:"headers"`
HeadersArray []map[string]string `json:"headersArray"`
JSON string `json:"json"`
OK bool `json:"ok"`
RequestURL string `json:"requestUrl"`
SecurityDetails map[string]string `json:"securityDetails"`
ServerAddr map[string]any `json:"serverAddr"`
Size map[string]int `json:"size"`
Status int64 `json:"status"`
StatusText string `json:"statusText"`
URL string `json:"url"`
Text string `json:"text"`
AllHeaders map[string]string `json:"allHeaders"`
Body string `json:"body"`
FrameURL string `json:"frameUrl"`
AcceptLanguageHeader string `json:"acceptLanguageHeader"`
AcceptLanguageHeaders []string `json:"acceptLanguageHeaders"`
Headers map[string]string `json:"headers"`
HeadersArray []map[string]string `json:"headersArray"`
JSON string `json:"json"`
OK bool `json:"ok"`
RequestURL string `json:"requestUrl"`
SecurityDetails common.SecurityDetails `json:"securityDetails"`
ServerAddr common.RemoteAddress `json:"serverAddr"`
Size map[string]int `json:"size"`
Status int64 `json:"status"`
StatusText string `json:"statusText"`
URL string `json:"url"`
Text string `json:"text"`
}

func TestPageOnResponse(t *testing.T) {
Expand Down Expand Up @@ -2578,49 +2579,32 @@ func TestPageOnResponse(t *testing.T) {
err = json.Unmarshal([]byte(got.Result().String()), &responses)
require.NoError(t, err)

// Normalize the date
for i := range responses {
// Normalize any port numbers in the string values to :8080
if responses[i].URL != "" {
responses[i].URL = regexp.MustCompile(`:\d+`).ReplaceAllString(responses[i].URL, ":8080")
}
if responses[i].FrameURL != "" {
responses[i].FrameURL = regexp.MustCompile(`:\d+`).ReplaceAllString(responses[i].FrameURL, ":8080")
}
for k, v := range responses[i].AllHeaders {
responses[i].AllHeaders[k] = regexp.MustCompile(`:\d+`).ReplaceAllString(v, ":8080")

// Normalize the date
for k := range responses[i].AllHeaders {
if strings.Contains(strings.ToLower(k), "date") {
responses[i].AllHeaders[k] = "Wed, 29 Jan 2025 09:00:00 GMT"
}
}
for k, v := range responses[i].Headers {
responses[i].Headers[k] = regexp.MustCompile(`:\d+`).ReplaceAllString(v, ":8080")

// Normalize the date
for k := range responses[i].Headers {
if strings.Contains(strings.ToLower(k), "date") {
responses[i].Headers[k] = "Wed, 29 Jan 2025 09:00:00 GMT"
}
}
for k, header := range responses[i].HeadersArray {
if header["value"] != "" {
responses[i].HeadersArray[k]["value"] = regexp.MustCompile(`:\d+`).ReplaceAllString(header["value"], ":8080")
}
// Normalize the date
if strings.Contains(strings.ToLower(header["name"]), "date") {
responses[i].HeadersArray[k]["value"] = "Wed, 29 Jan 2025 09:00:00 GMT"
}
}
for k := range responses[i].ServerAddr {
if k == "port" {
responses[i].ServerAddr[k] = 8080
}
}
if responses[i].RequestURL != "" {
responses[i].RequestURL = regexp.MustCompile(`:\d+`).ReplaceAllString(responses[i].RequestURL, ":8080")
}
}

serverURL := tb.http.ServerHTTP.URL
host, p, err := net.SplitHostPort(strings.TrimPrefix(serverURL, "http://"))
require.NoError(t, err)

port, err := strconv.ParseInt(p, 10, 64)
require.NoError(t, err)

expected := []response{
{
AllHeaders: map[string]string{
Expand All @@ -2629,7 +2613,7 @@ func TestPageOnResponse(t *testing.T) {
"date": "Wed, 29 Jan 2025 09:00:00 GMT",
},
Body: "<!DOCTYPE html>\n<html>\n<head>\n <link rel=\"stylesheet\" href=\"/style.css\">\n</head>\n<body>\n <script>fetch('/api', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json'\n },\n body: JSON.stringify({name: 'tester'})\n })</script>\n</body>\n</html>",
FrameURL: "http://127.0.0.1:8080/home",
FrameURL: tb.url("/home"),
AcceptLanguageHeader: "",
AcceptLanguageHeaders: []string{""},
Headers: map[string]string{
Expand All @@ -2644,13 +2628,13 @@ func TestPageOnResponse(t *testing.T) {
},
JSON: "null",
OK: true,
RequestURL: "http://127.0.0.1:8080/home",
SecurityDetails: map[string]string(nil),
ServerAddr: map[string]interface{}{"ip_address": "127.0.0.1", "port": 8080},
RequestURL: tb.url("/home"),
SecurityDetails: common.SecurityDetails{},
ServerAddr: common.RemoteAddress{IPAddress: host, Port: port},
Size: map[string]int{"body": 286, "headers": 117},
Status: 200,
StatusText: "OK",
URL: "http://127.0.0.1:8080/home",
URL: tb.url("/home"),
Text: "<!DOCTYPE html>\n<html>\n<head>\n <link rel=\"stylesheet\" href=\"/style.css\">\n</head>\n<body>\n <script>fetch('/api', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json'\n },\n body: JSON.stringify({name: 'tester'})\n })</script>\n</body>\n</html>",
},
{
Expand All @@ -2660,7 +2644,7 @@ func TestPageOnResponse(t *testing.T) {
"date": "Wed, 29 Jan 2025 09:00:00 GMT",
},
Body: "body { background-color: #f0f0f0; }",
FrameURL: "http://127.0.0.1:8080/home",
FrameURL: tb.url("/home"),
AcceptLanguageHeader: "",
AcceptLanguageHeaders: []string{""},
Headers: map[string]string{
Expand All @@ -2675,13 +2659,13 @@ func TestPageOnResponse(t *testing.T) {
},
JSON: "null",
OK: true,
RequestURL: "http://127.0.0.1:8080/style.css",
SecurityDetails: map[string]string(nil),
ServerAddr: map[string]interface{}{"ip_address": "127.0.0.1", "port": 8080},
RequestURL: tb.url("/style.css"),
SecurityDetails: common.SecurityDetails{},
ServerAddr: common.RemoteAddress{IPAddress: host, Port: port},
Size: map[string]int{"body": 35, "headers": 100},
Status: 200,
StatusText: "OK",
URL: "http://127.0.0.1:8080/style.css",
URL: tb.url("/style.css"),
Text: "body { background-color: #f0f0f0; }",
},
{
Expand All @@ -2694,7 +2678,7 @@ func TestPageOnResponse(t *testing.T) {
"x-content-type-options": "nosniff",
},
Body: "Not Found\n",
FrameURL: "http://127.0.0.1:8080/home",
FrameURL: tb.url("/home"),
AcceptLanguageHeader: "",
AcceptLanguageHeaders: []string{""},
Headers: map[string]string{
Expand All @@ -2715,13 +2699,13 @@ func TestPageOnResponse(t *testing.T) {
},
JSON: "null",
OK: false,
RequestURL: "http://127.0.0.1:8080/favicon.ico",
SecurityDetails: map[string]string(nil),
ServerAddr: map[string]interface{}{"ip_address": "127.0.0.1", "port": 8080},
RequestURL: tb.url("/favicon.ico"),
SecurityDetails: common.SecurityDetails{},
ServerAddr: common.RemoteAddress{IPAddress: host, Port: port},
Size: map[string]int{"body": 10, "headers": 229},
Status: 404,
StatusText: "Not Found",
URL: "http://127.0.0.1:8080/favicon.ico",
URL: tb.url("/favicon.ico"),
Text: "Not Found\n",
},
{
Expand All @@ -2731,7 +2715,7 @@ func TestPageOnResponse(t *testing.T) {
"date": "Wed, 29 Jan 2025 09:00:00 GMT",
},
Body: "{\"message\": \"Hello tester!\"}",
FrameURL: "http://127.0.0.1:8080/home",
FrameURL: tb.url("/home"),
AcceptLanguageHeader: "",
AcceptLanguageHeaders: []string{""},
Headers: map[string]string{
Expand All @@ -2746,13 +2730,13 @@ func TestPageOnResponse(t *testing.T) {
},
JSON: "{\"message\":\"Hello tester!\"}",
OK: true,
RequestURL: "http://127.0.0.1:8080/api",
SecurityDetails: map[string]string(nil),
ServerAddr: map[string]interface{}{"ip_address": "127.0.0.1", "port": 8080},
RequestURL: tb.url("/api"),
SecurityDetails: common.SecurityDetails{},
ServerAddr: common.RemoteAddress{IPAddress: host, Port: port},
Size: map[string]int{"body": 28, "headers": 108},
Status: 200,
StatusText: "OK",
URL: "http://127.0.0.1:8080/api",
URL: tb.url("/api"),
Text: "{\"message\": \"Hello tester!\"}",
},
}
Expand Down

0 comments on commit fea5839

Please sign in to comment.