From a16a847b26838fa4d87c8cc16405e7c6d129acf1 Mon Sep 17 00:00:00 2001 From: JP Hastings-Spital Date: Sat, 14 Dec 2024 10:12:55 +0000 Subject: [PATCH] fix: TinyGo compatible packages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The net/http/httptest package isn't implemented in tinygo — this (minor) change swaps to creating the necessary http.Request struct directly (which needs an extra step or two). This *is not* enough to get TinyGo _working_ as a compiler for this repo, but the compilation succeeds now. --- request.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/request.go b/request.go index 5f414f6..c8e82e8 100644 --- a/request.go +++ b/request.go @@ -3,7 +3,7 @@ package wasmhttp import ( "io" "net/http" - "net/http/httptest" + "net/url" "syscall/js" promise "github.com/nlepage/go-js-promise" @@ -20,7 +20,11 @@ func Request(uvalue js.Value) (*http.Request, error) { return nil, err } - url, err := value.GetString("url") + rawURL, err := value.GetString("url") + if err != nil { + return nil, err + } + u, err := url.Parse(rawURL) if err != nil { return nil, err } @@ -59,11 +63,12 @@ func Request(uvalue js.Value) (*http.Request, error) { bodyReader = readablestream.NewReader(r) } - req := httptest.NewRequest( - method, - url, - bodyReader, - ) + req := &http.Request{ + Method: method, + URL: u, + Body: io.NopCloser(bodyReader), + Header: make(http.Header), + } headers, err := value.Get("headers") if err != nil {