(t *testing.T)
| 1403 | } |
| 1404 | |
| 1405 | func TestBindHTTPSend(t *testing.T) { |
| 1406 | t.Parallel() |
| 1407 | |
| 1408 | // start a test server |
| 1409 | server := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 1410 | if req.URL.Query().Get("testError") != "" { |
| 1411 | res.WriteHeader(400) |
| 1412 | return |
| 1413 | } |
| 1414 | |
| 1415 | timeoutStr := req.URL.Query().Get("testTimeout") |
| 1416 | timeout, _ := strconv.Atoi(timeoutStr) |
| 1417 | if timeout > 0 { |
| 1418 | time.Sleep(time.Duration(timeout) * time.Second) |
| 1419 | } |
| 1420 | |
| 1421 | bodyRaw, _ := io.ReadAll(req.Body) |
| 1422 | defer req.Body.Close() |
| 1423 | |
| 1424 | // normalize headers |
| 1425 | headers := make(map[string]string, len(req.Header)) |
| 1426 | for k, v := range req.Header { |
| 1427 | if len(v) > 0 { |
| 1428 | headers[strings.ToLower(strings.ReplaceAll(k, "-", "_"))] = v[0] |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | info := map[string]any{ |
| 1433 | "method": req.Method, |
| 1434 | "headers": headers, |
| 1435 | "body": string(bodyRaw), |
| 1436 | } |
| 1437 | |
| 1438 | // add custom headers and cookies |
| 1439 | res.Header().Add("X-Custom", "custom_header") |
| 1440 | res.Header().Add("Set-Cookie", "sessionId=123456") |
| 1441 | |
| 1442 | infoRaw, _ := json.Marshal(info) |
| 1443 | |
| 1444 | // write back the submitted request |
| 1445 | res.Write(infoRaw) |
| 1446 | })) |
| 1447 | defer server.Close() |
| 1448 | |
| 1449 | vm := goja.New() |
| 1450 | BindCore(vm) |
| 1451 | BindHTTP(vm) |
| 1452 | vm.Set("testURL", server.URL) |
| 1453 | |
| 1454 | _, err := vm.RunString(` |
| 1455 | function getNestedVal(data, path) { |
| 1456 | let result = data || {}; |
| 1457 | let parts = path.split("."); |
| 1458 | |
| 1459 | for (const part of parts) { |
| 1460 | if ( |
| 1461 | result == null || |
| 1462 | typeof result !== "object" || |
nothing calls this directly
no test coverage detected
searching dependent graphs…