Skip to content

Commit 83fe38e

Browse files
committed
fix: unit tests
Signed-off-by: Imran Pochi <imranpochi@microsoft.com>
1 parent 4d6bd6b commit 83fe38e

File tree

1 file changed

+11
-51
lines changed

1 file changed

+11
-51
lines changed

tests/proxy_test.go

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ func TestFailedDNSLookupProxy_HTTPCONN(t *testing.T) {
689689
t.Error(err)
690690
}
691691

692-
urlString := "http://thissssssxxxxx.com:80"
692+
urlString := "http://thisdefinitelydoesnotexist.com:80"
693693
serverURL, _ := url.Parse(urlString)
694694

695695
// Send HTTP-Connect request
@@ -705,36 +705,12 @@ func TestFailedDNSLookupProxy_HTTPCONN(t *testing.T) {
705705
t.Errorf("reading HTTP response from CONNECT: %v", err)
706706
}
707707

708-
if res.StatusCode != 200 {
709-
t.Errorf("expect 200; got %d", res.StatusCode)
710-
}
711-
if br.Buffered() > 0 {
712-
t.Error("unexpected extra buffer")
713-
}
714-
dialer := func(_, _ string) (net.Conn, error) {
715-
return conn, nil
716-
}
717-
718-
c := &http.Client{
719-
Transport: &http.Transport{
720-
Dial: dialer,
721-
},
722-
}
723-
724-
resp, err := c.Get(urlString)
725-
if err != nil {
726-
t.Error(err)
708+
if res.StatusCode != 502 {
709+
t.Errorf("expect 502; got %d", res.StatusCode)
727710
}
728711

729-
if resp.StatusCode != 503 {
730-
t.Errorf("expect 503; got %d", res.StatusCode)
731-
}
732-
733-
body, err := io.ReadAll(resp.Body)
734-
resp.Body.Close()
735-
if !strings.Contains(err.Error(), "connection reset by peer") {
736-
t.Error(err)
737-
}
712+
body, err := io.ReadAll(res.Body)
713+
res.Body.Close()
738714

739715
if !strings.Contains(string(body), "no such host") {
740716
t.Errorf("Unexpected error: %v", err)
@@ -779,37 +755,21 @@ func TestFailedDial_HTTPCONN(t *testing.T) {
779755
br := bufio.NewReader(conn)
780756
res, err := http.ReadResponse(br, nil)
781757
if err != nil {
782-
t.Fatalf("reading HTTP response from CONNECT: %v", err)
783-
}
784-
if res.StatusCode != 200 {
785-
t.Fatalf("expect 200; got %d", res.StatusCode)
758+
t.Errorf("reading HTTP response from CONNECT: %v", err)
786759
}
787760

788-
dialer := func(_, _ string) (net.Conn, error) {
789-
return conn, nil
761+
if res.StatusCode != 502 {
762+
t.Errorf("expect 502; got %d", res.StatusCode)
790763
}
791764

792-
c := &http.Client{
793-
Transport: &http.Transport{
794-
Dial: dialer,
795-
},
796-
}
797-
798-
resp, err := c.Get(server.URL)
765+
body, err := io.ReadAll(res.Body)
766+
res.Body.Close()
799767
if err != nil {
800-
t.Fatal(err)
801-
}
802-
803-
body, err := io.ReadAll(resp.Body)
804-
resp.Body.Close()
805-
if err == nil {
806768
t.Fatalf("Expected error reading response body; response=%q", body)
807-
} else if !strings.Contains(err.Error(), "connection reset by peer") {
808-
t.Error(err)
809769
}
810770

811771
if !strings.Contains(string(body), "connection refused") {
812-
t.Errorf("Unexpected error: %v", err)
772+
t.Errorf("Expected 'connection refused' in error body, got: %s", string(body))
813773
}
814774

815775
if err := ps.Metrics().ExpectServerDialFailure(metricsserver.DialFailureErrorResponse, 1); err != nil {

0 commit comments

Comments
 (0)