Skip to content

Commit

Permalink
fix probe_resist_test.go test cases, the repose header['Alt-Svc'] is …
Browse files Browse the repository at this point in the history
…different from different server

Signed-off-by: johnzhanghua <[email protected]>
  • Loading branch information
johnzhanghua committed Sep 25, 2023
1 parent 315436f commit b6e5de1
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions probe_resist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ func TestGETAuthWrongProbeResist(t *testing.T) {
responseReference.StatusCode, responseProbeResist.StatusCode)
}
if err = responsesAreEqual(responseProbeResist, responseReference); err != nil {
t.Fatal(err)
var e errorHeaderAlternativeServiceNotEqual
if !errors.As(err, &e) {
t.Fatal(err)
}
if err = e.CheckAlternativeServiceError(caddyForwardProxyProbeResist.addr, caddyDummyProbeResist.addr); err != nil {
t.Fatal(err)
}
}
if err = responsesAreEqual(responseProbeResist, responseForwardProxy); err == nil {
t.Fatalf("Responses from servers with and without Probe Resistance are expected to be different."+
Expand Down Expand Up @@ -78,7 +84,13 @@ func TestGETAuthWrongProbeResist(t *testing.T) {
responseProbeResist.StatusCode)
}
if err = responsesAreEqual(responseProbeResist, responseReference); err != nil {
t.Fatal(err)
var e errorHeaderAlternativeServiceNotEqual
if !errors.As(err, &e) {
t.Fatal(err)
}
if err = e.CheckAlternativeServiceError(caddyForwardProxyProbeResist.addr, caddyDummyProbeResist.addr); err != nil {
t.Fatal(err)
}
}
if err = responsesAreEqual(responseProbeResist, responseForwardProxy); err == nil {
t.Fatalf("Responses from servers with and without Probe Resistance are expected to be different."+
Expand Down Expand Up @@ -174,7 +186,13 @@ func TestConnectAuthWrongProbeResist(t *testing.T) {
responseReference.StatusCode, responseProbeResist.StatusCode)
}
if err = responsesAreEqual(responseProbeResist, responseReference); err != nil {
t.Fatal(err)
var e errorHeaderAlternativeServiceNotEqual
if !errors.As(err, &e) {
t.Fatal(err)
}
if err = e.CheckAlternativeServiceError(caddyForwardProxyProbeResist.addr, caddyDummyProbeResist.addr); err != nil {
t.Fatal(err)
}
}
if err = responsesAreEqual(responseProbeResist, responseForwardProxy); err == nil {
t.Fatalf("Responses from servers with and without Probe Resistance are expected to be different."+
Expand Down Expand Up @@ -202,7 +220,13 @@ func TestConnectAuthWrongProbeResist(t *testing.T) {
t.Fatal(err)
}
if err = responsesAreEqual(responseProbeResist, responseReference); err != nil {
t.Fatal(err)
var e errorHeaderAlternativeServiceNotEqual
if !errors.As(err, &e) {
t.Fatal(err)
}
if err = e.CheckAlternativeServiceError(caddyForwardProxyProbeResist.addr, caddyDummyProbeResist.addr); err != nil {
t.Fatal(err)
}
}
if err = responsesAreEqual(responseProbeResist, responseForwardProxy); err == nil {
t.Fatalf("Responses from servers with and without Probe Resistance are expected to be different."+
Expand Down Expand Up @@ -263,6 +287,37 @@ func TestConnectAuthWrongProbeResistRedir(t *testing.T) {
}
}

type errorHeaderAlternativeServiceNotEqual struct {
ValueA []string
ValueB []string
}

func (e errorHeaderAlternativeServiceNotEqual) Error() string {
return fmt.Sprintf("header 'Alt-Svc' not equal: %v, %v\n", e.ValueA, e.ValueB)
}

func (e errorHeaderAlternativeServiceNotEqual) CheckAlternativeServiceError(serverAddrA, serverAddrB string) error {
if len(e.ValueA) == 0 || len(e.ValueB) == 0 {
return fmt.Errorf("header 'Alt-Svc' is empty: %w", e)
}
_, port, err := net.SplitHostPort(serverAddrA)
if err != nil {
return fmt.Errorf("failed to split server address :%w", err)
}
if !strings.Contains(e.ValueA[0], port) {
return fmt.Errorf("Alt-Svc address :%s does not contain the server port: %s", e.ValueA[0], port)
}
_, port, err = net.SplitHostPort(serverAddrB)
if err != nil {
return fmt.Errorf("failed to split server address :%w", err)
}
if !strings.Contains(e.ValueB[0], port) {
return fmt.Errorf("Alt-Svc address :%s does not contain the server port: %s", e.ValueB[0], port)
}

return nil
}

// returns nil if are equal
func responsesAreEqual(res1, res2 *http.Response) error {
if res1 == nil {
Expand Down Expand Up @@ -343,6 +398,9 @@ func responsesAreEqual(res1, res2 *http.Response) error {
return fmt.Errorf("header \"%s: %s\" is absent in res2", k1, v1)
}
if errStr = stringSlicesAreEqual(v1, v2); errStr != "" {
if k1 == "Alt-Svc" {
return errorHeaderAlternativeServiceNotEqual{v1, v2}
}
return fmt.Errorf("header \"%s\" is different: %s", k1, errStr)
}
}
Expand Down

0 comments on commit b6e5de1

Please sign in to comment.