[#3310] added headers and cookies fields to the .send result

This commit is contained in:
Gani Georgiev
2023-09-14 14:45:05 +03:00
parent 2608efb56c
commit bb0a2dd698
36 changed files with 3760 additions and 3731 deletions
+15 -3
View File
@@ -613,9 +613,11 @@ func httpClientBinds(vm *goja.Runtime) {
vm.Set("$http", obj)
type sendResult struct {
StatusCode int
Raw string
Json any
StatusCode int `json:"statusCode"`
Headers map[string][]string `json:"headers"`
Cookies map[string]*http.Cookie `json:"cookies"`
Raw string `json:"raw"`
Json any `json:"json"`
}
type sendConfig struct {
@@ -686,9 +688,19 @@ func httpClientBinds(vm *goja.Runtime) {
result := &sendResult{
StatusCode: res.StatusCode,
Headers: map[string][]string{},
Cookies: map[string]*http.Cookie{},
Raw: string(bodyRaw),
}
for k, v := range res.Header {
result.Headers[k] = v
}
for _, v := range res.Cookies() {
result.Cookies[v.Name] = v
}
if len(result.Raw) != 0 {
// try as map
result.Json = map[string]any{}
+11 -3
View File
@@ -1023,9 +1023,9 @@ func TestHttpClientBindsCount(t *testing.T) {
func TestHttpClientBindsSend(t *testing.T) {
// start a test server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
server := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Query().Get("testError") != "" {
rw.WriteHeader(400)
res.WriteHeader(400)
return
}
@@ -1052,10 +1052,14 @@ func TestHttpClientBindsSend(t *testing.T) {
"body": string(bodyRaw),
}
// add custom headers and cookies
res.Header().Add("X-Custom", "custom_header")
res.Header().Add("Set-Cookie", "sessionId=123456")
infoRaw, _ := json.Marshal(info)
// write back the submitted request
rw.Write(infoRaw)
res.Write(infoRaw)
}))
defer server.Close()
@@ -1122,6 +1126,8 @@ func TestHttpClientBindsSend(t *testing.T) {
}],
[test1, {
"statusCode": "200",
"headers.X-Custom.0": "custom_header",
"cookies.sessionId.value": "123456",
"json.method": "POST",
"json.headers.header1": "123",
"json.headers.header2": "456",
@@ -1130,6 +1136,8 @@ func TestHttpClientBindsSend(t *testing.T) {
}],
[test2, {
"statusCode": "200",
"headers.X-Custom.0": "custom_header",
"cookies.sessionId.value": "123456",
"json.method": "GET",
"json.headers.content_type": "text/plain",
}],
File diff suppressed because it is too large Load Diff
+5 -3
View File
@@ -896,9 +896,11 @@ declare namespace $http {
// deprecated, please use body instead
data?: { [key:string]: any },
}): {
statusCode: number
raw: string
json: any
statusCode: number,
headers: { [key:string]: Array<string> },
cookies: { [key:string]: http.Cookie },
raw: string,
json: any,
};
}