blade_1
blade_1

Reputation: 53

dial tcp: Protocol not available go webassembly test

Attempting to go test a web-assembly function which fires an POST request.

Receive the following error:

firePing_test.go:40: ERROR ON POST REQUEST: Post https://not-the-real-api.execute-api.us-east-1.amazonaws.com/testing: dial tcp: Protocol not available

Running: Ubuntu 18.04.2 LTS go version go1.12.2 linux/amd64

I've tested that the function is valid and will send a request when executing in chrome. Test also passes when compiled for linux/amd64.

Problem Function:

// FirePing fires a ping
func FirePing(protocol *string, domain *string, params *map[string]string) (*http.Response, error) {

    // Marshal map into POST request body
    reqBody, err := json.Marshal(*params)
    if err != nil {
        return  nil, fmt.Errorf("ERROR ON MARSHAL OF PARAMS: %v", err)
    }

    // Send POST request
    req, err := http.NewRequest("POST", *protocol + "://" + *domain, bytes.NewBuffer(reqBody))
    if err != nil {
        return  nil, fmt.Errorf("ERROR ON FORMING REQUEST: %v", err)
    }
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return nil,fmt.Errorf("ERROR ON POST REQUEST: %v",err)
    }

    return resp, nil
}

Problem Test Function Call:

// FirePing and receive response
    resp, err := FirePing(&config.Config.Protocol, &config.Config.Domain, &m)
    if err != nil {
        t.Error(err)
        return
    }

Should pass this test case as it performs the function call fine in the browser.

Only other place I have seen this is:

http.Get returns Protocol not available error

Which seams to be from playground disabling tcp connections. I am running this test locally

Upvotes: 3

Views: 1299

Answers (1)

jonathanberi
jonathanberi

Reputation: 1947

The MVP release was exclusively targeting running WASM in the browser. As such, the browser APIs do not allow arbitrary TCP/IP connections and therefore WASM is equally limited. That said, WASM has always had ambitions to go beyond the browser. The latest initiative called WASI will add features required to be used outside the browser, like File I/O and networking primitives.

Upvotes: 5

Related Questions