Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPRequest #3

Merged
merged 14 commits into from
Jun 22, 2021
Prev Previous commit
Next Next commit
HTTPResponse.Body
  • Loading branch information
orsinium committed Jun 22, 2021
commit 18cc99b7cc181d762061c68d7837faa76d35a788
28 changes: 23 additions & 5 deletions web/http_request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"encoding/base64"
"strings"
"sync"
"time"
Expand All @@ -11,6 +12,7 @@ import (
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/XMLHttpRequest
type HTTPRequest struct {
Value
window Window
}

// Send the HTTP request. This operation is blocking on the Go side
Expand All @@ -25,9 +27,11 @@ func (req HTTPRequest) Send(body []byte) HTTPResponse {
})

if body == nil {
req.Call("send", "")
req.Call("send", nil)
} else {
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob
encoded := base64.StdEncoding.EncodeToString(body)
req.Call("send", req.window.Call("atob", encoded))
}

wg.Wait()
Expand Down Expand Up @@ -65,7 +69,23 @@ func (req HTTPRequest) SetHeader(header, value string) {
}

type HTTPResponse struct {
value Value
value Value
window Window
}

// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/response
func (resp HTTPResponse) Body() []byte {
raw := resp.value.Get("response")
if raw.IsNull() {
return nil
}
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa
enc := resp.window.Call("btoa", raw).String()
dec, err := base64.StdEncoding.DecodeString(enc)
if err != nil {
panic(err)
}
return dec
}

// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseText
Expand All @@ -90,7 +110,7 @@ func (resp HTTPResponse) Status() string {
}

func (resp HTTPResponse) Headers() Headers {
return Headers(resp)
return Headers{value: resp.value}
}

// Encapsulates methods to work with HTTP response headers.
Expand All @@ -115,5 +135,3 @@ func (h Headers) Values() []string {
vals := h.value.Call("getAllResponseHeaders").String()
return strings.Split(strings.TrimSpace(vals), "\r\n")
}

// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa
19 changes: 19 additions & 0 deletions web/http_request_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package web

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -15,3 +16,21 @@ func TestHTTPRequest_GET(t *testing.T) {
is.Equal(resp.Headers().Get("Content-Type"), "application/json")
is.Equal(resp.Headers().Values(), []string{"content-length: 619", "content-type: application/json"})
}

func TestHTTPRequest_POST(t *testing.T) {
is := require.New(t)
req := GetWindow().HTTPRequest("POST", "https://httpbin.org/post")
resp := req.Send([]byte("hello=world"))
is.Equal(resp.StatusCode(), 200)
is.Equal(resp.Status(), "")
is.Equal(resp.Headers().Get("Content-Type"), "application/json")
is.Equal(resp.Headers().Values(), []string{"content-length: 772", "content-type: application/json"})
is.Contains(resp.Text(), `"form":`)

data := struct {
Form map[string]string `json:"form"`
}{}
err := json.Unmarshal(resp.Body(), &data)
is.Nil(err)
is.Equal(data.Form["hello"], "world")
}
5 changes: 4 additions & 1 deletion web/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ func (window Window) Screen() Screen {
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType
func (window Window) HTTPRequest(method, url string) HTTPRequest {
req := HTTPRequest{Value: window.Get("XMLHttpRequest").New()}
req := HTTPRequest{
Value: window.Get("XMLHttpRequest").New(),
window: window,
}
req.Call("open", method, url, true)
req.Set("responseType", "blob")
return req
Expand Down