mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-09-21 18:50:13 +00:00
test(agentcompat): add integration scenarios
Co-authored-by: naiba/CloudCode <hi+cloudcode@nai.ba>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
//go:build linux
|
||||
|
||||
package scenario
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/nezhahq/nezha/integration/agentcompat/internal/client"
|
||||
"github.com/nezhahq/nezha/pkg/agentcompatcontract"
|
||||
)
|
||||
|
||||
type heldNATRequest struct {
|
||||
connection net.Conn
|
||||
result chan error
|
||||
closeOnce sync.Once
|
||||
closeErr error
|
||||
}
|
||||
|
||||
func startHeldNATRequest(ctx context.Context, endpoint, domain, identity string, capability client.IOStreamCapability) (*heldNATRequest, error) {
|
||||
connection, err := (&net.Dialer{}).DialContext(ctx, "tcp", endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
request := &heldNATRequest{connection: connection, result: make(chan error, 1)}
|
||||
method := "PATCH"
|
||||
path := "/held/" + identity
|
||||
body := "held-body-" + identity
|
||||
go func() {
|
||||
wire := fmt.Sprintf("%s %s HTTP/1.1\r\nHost: %s\r\nX-AgentCompat-Echo: %s\r\n%s: %s\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", method, path, domain, identity, agentcompatcontract.IOStreamCapabilityHeader, capability.Value(), len(body), body)
|
||||
_, requestErr := io.WriteString(connection, wire)
|
||||
if requestErr == nil {
|
||||
response, readErr := http.ReadResponse(bufio.NewReader(connection), nil)
|
||||
if readErr == nil {
|
||||
_, readErr = io.Copy(io.Discard, response.Body)
|
||||
closeErr := response.Body.Close()
|
||||
if readErr == nil {
|
||||
readErr = closeErr
|
||||
}
|
||||
}
|
||||
requestErr = readErr
|
||||
}
|
||||
request.result <- requestErr
|
||||
}()
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func (request *heldNATRequest) close() error {
|
||||
request.closeOnce.Do(func() { request.closeErr = request.connection.Close() })
|
||||
return request.closeErr
|
||||
}
|
||||
Reference in New Issue
Block a user