61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package inbound
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewAPIHTTPClientMatchesBoundedSubmitConcurrency(t *testing.T) {
|
|
client := NewAPIHTTPClient(48)
|
|
transport, ok := client.Transport.(*http.Transport)
|
|
if !ok {
|
|
t.Fatalf("expected *http.Transport, got %T", client.Transport)
|
|
}
|
|
if transport.MaxConnsPerHost != 48 || transport.MaxIdleConnsPerHost != 48 {
|
|
t.Fatalf("unexpected host connection bounds: max=%d idle=%d", transport.MaxConnsPerHost, transport.MaxIdleConnsPerHost)
|
|
}
|
|
if client.Timeout != defaultHTTPTimeout {
|
|
t.Fatalf("unexpected client timeout: %s", client.Timeout)
|
|
}
|
|
}
|
|
|
|
func TestNewAPIHTTPClientUsesSafeDefault(t *testing.T) {
|
|
client := NewAPIHTTPClient(0)
|
|
transport := client.Transport.(*http.Transport)
|
|
if transport.MaxConnsPerHost != 64 {
|
|
t.Fatalf("expected default max connections 64, got %d", transport.MaxConnsPerHost)
|
|
}
|
|
}
|
|
|
|
func TestSubmitUsesDedicatedHTTPClient(t *testing.T) {
|
|
background := &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
|
|
t.Fatal("background client must not carry inbound Submit")
|
|
return nil, nil
|
|
})}
|
|
submit := &http.Client{Transport: roundTripFunc(func(request *http.Request) (*http.Response, error) {
|
|
if request.URL.Path != "/api/gateway/events/inbound/submit" {
|
|
t.Fatalf("unexpected submit path %s", request.URL.Path)
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusCreated,
|
|
Status: "201 Created",
|
|
Header: make(http.Header),
|
|
Body: io.NopCloser(strings.NewReader(`{"accepted":true,"messageId":"MSG-1"}`)),
|
|
}, nil
|
|
})}
|
|
server := Server{APIBaseURL: "http://api.test/api", HTTPClient: background, SubmitHTTPClient: submit}
|
|
result, err := server.submit(&net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 12000}, submitRequest{Account: "test"})
|
|
if err != nil || !result.Accepted {
|
|
t.Fatalf("expected dedicated submit success, result=%+v err=%v", result, err)
|
|
}
|
|
}
|
|
|
|
type roundTripFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (fn roundTripFunc) RoundTrip(request *http.Request) (*http.Response, error) {
|
|
return fn(request)
|
|
}
|