mirror of
https://github.com/adnanh/webhook.git
synced 2025-05-15 10:04:44 +00:00
Refactored webhook_test so that the test HTTP requests are made using an explicitly-provided http.Client, so we can run at least one test with the server bound to a socket instead of a port number, using an http.Client whose transport has been configured with a suitable Unix-domain or Windows named pipe dialer function.
30 lines
665 B
Go
30 lines
665 B
Go
//go:build !windows
|
|
// +build !windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
func prepareTestSocket(_ string) (socketPath string, transport *http.Transport, cleanup func(), err error) {
|
|
tmp, err := ioutil.TempDir("", "webhook-socket-")
|
|
if err != nil {
|
|
return "", nil, nil, err
|
|
}
|
|
cleanup = func() { os.RemoveAll(tmp) }
|
|
socketPath = path.Join(tmp, "webhook.sock")
|
|
socketDialer := &net.Dialer{}
|
|
transport = &http.Transport{
|
|
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
|
return socketDialer.DialContext(ctx, "unix", socketPath)
|
|
},
|
|
}
|
|
|
|
return socketPath, transport, cleanup, nil
|
|
}
|