Skip to content

Commit 337925b

Browse files
committed
chore(tst/streamSrv): update ProtocolVersionHeader test
1 parent 0596c88 commit 337925b

File tree

1 file changed

+65
-55
lines changed

1 file changed

+65
-55
lines changed

server/streamable_http_test.go

Lines changed: 65 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -839,124 +839,134 @@ func TestStreamableHTTPServer_ProtocolVersionHeader(t *testing.T) {
839839
rawNotification, _ := json.Marshal(notification)
840840

841841
// Request with protocol version to be the one negotiated during initialization.
842-
req, _ := http.NewRequest(http.MethodPost, server.URL, bytes.NewBuffer(rawNotification))
843-
req.Header.Set("Content-Type", "application/json")
844-
req.Header.Set(headerKeyProtocolVersion, clientInitializedProtocolVersion)
845-
resp, err := server.Client().Do(req)
842+
req1, _ := http.NewRequest(http.MethodPost, server.URL, bytes.NewBuffer(rawNotification))
843+
req1.Header.Set("Content-Type", "application/json")
844+
req1.Header.Set(headerKeyProtocolVersion, clientInitializedProtocolVersion)
845+
resp1, err := server.Client().Do(req1)
846846
if err != nil {
847847
t.Fatalf("Failed to send message: %v", err)
848848
}
849-
if resp.StatusCode != http.StatusAccepted {
850-
t.Errorf("Expected status 202, got %d", resp.StatusCode)
849+
defer resp1.Body.Close()
850+
if resp1.StatusCode != http.StatusAccepted {
851+
t.Errorf("Expected status 202, got %d", resp1.StatusCode)
851852
}
852-
bodyBytes, _ := io.ReadAll(resp.Body)
853+
bodyBytes, _ := io.ReadAll(resp1.Body)
853854
if len(bodyBytes) > 0 {
854855
t.Errorf("Expected empty body, got %s", string(bodyBytes))
855856
}
856-
resp.Body.Close()
857857

858858
// Request with protocol version not to be the one negotiated during initialization but supported by server.
859-
req.Header.Set(headerKeyProtocolVersion, "2024-11-05")
860-
resp, err = server.Client().Do(req)
859+
req2, _ := http.NewRequest(http.MethodPost, server.URL, bytes.NewBuffer(rawNotification))
860+
req2.Header.Set("Content-Type", "application/json")
861+
req2.Header.Set(headerKeyProtocolVersion, "2024-11-05")
862+
resp2, err := server.Client().Do(req2)
861863
if err != nil {
862864
t.Fatalf("Failed to send message: %v", err)
863865
}
864-
if resp.StatusCode != http.StatusAccepted {
865-
t.Errorf("Expected status 202, got %d", resp.StatusCode)
866+
defer resp2.Body.Close()
867+
if resp2.StatusCode != http.StatusAccepted {
868+
t.Errorf("Expected status 202, got %d", resp2.StatusCode)
866869
}
867-
bodyBytes, _ = io.ReadAll(resp.Body)
870+
bodyBytes, _ = io.ReadAll(resp2.Body)
868871
if len(bodyBytes) > 0 {
869872
t.Errorf("Expected empty body, got %s", string(bodyBytes))
870873
}
871-
resp.Body.Close()
872874

873875
// Request with protocol version not to be the one negotiated during initialization but also invalid/unsupported by server.
874-
req.Header.Set(headerKeyProtocolVersion, "2024-06-18")
875-
resp, err = server.Client().Do(req)
876+
req3, _ := http.NewRequest(http.MethodPost, server.URL, bytes.NewBuffer(rawNotification))
877+
req3.Header.Set("Content-Type", "application/json")
878+
req3.Header.Set(headerKeyProtocolVersion, "2024-06-18")
879+
resp3, err := server.Client().Do(req3)
876880
if err != nil {
877881
t.Fatalf("Failed to send message: %v", err)
878882
}
879-
if resp.StatusCode != http.StatusBadRequest {
880-
t.Errorf("Expected status 400, got %d", resp.StatusCode)
883+
defer resp3.Body.Close()
884+
if resp3.StatusCode != http.StatusBadRequest {
885+
t.Errorf("Expected status 400, got %d", resp3.StatusCode)
881886
}
882-
resp.Body.Close()
883887
})
884888

885889
t.Run("GET Request", func(t *testing.T) {
886890
// Request with protocol version to be the one negotiated during initialization.
887-
req, _ := http.NewRequest(http.MethodGet, server.URL, nil)
888-
req.Header.Set("Content-Type", "text/event-stream")
889-
req.Header.Set(headerKeyProtocolVersion, clientInitializedProtocolVersion)
890-
resp, err := http.DefaultClient.Do(req)
891+
req1, _ := http.NewRequest(http.MethodGet, server.URL, nil)
892+
req1.Header.Set("Content-Type", "text/event-stream")
893+
req1.Header.Set(headerKeyProtocolVersion, clientInitializedProtocolVersion)
894+
resp1, err := http.DefaultClient.Do(req1)
891895
if err != nil {
892896
t.Fatalf("Failed to send message: %v\n", err)
893897
}
894-
if resp.StatusCode != http.StatusOK {
895-
bodyBytes, _ := io.ReadAll(resp.Body)
896-
t.Fatalf("Expected status 200, got %d, Response:%s", resp.StatusCode, string(bodyBytes))
898+
defer resp1.Body.Close()
899+
if resp1.StatusCode != http.StatusOK {
900+
bodyBytes, _ := io.ReadAll(resp1.Body)
901+
t.Fatalf("Expected status 200, got %d, Response:%s", resp1.StatusCode, string(bodyBytes))
897902
}
898-
resp.Body.Close()
899903

900904
// Request with protocol version not to be the one negotiated during initialization but supported by server.
901-
req.Header.Set(headerKeyProtocolVersion, "2024-11-05")
902-
resp, err = http.DefaultClient.Do(req)
905+
req2, _ := http.NewRequest(http.MethodGet, server.URL, nil)
906+
req2.Header.Set("Content-Type", "text/event-stream")
907+
req2.Header.Set(headerKeyProtocolVersion, "2024-11-05")
908+
resp2, err := http.DefaultClient.Do(req2)
903909
if err != nil {
904910
t.Fatalf("Failed to send message: %v\n", err)
905911
}
906-
if resp.StatusCode != http.StatusOK {
907-
bodyBytes, _ := io.ReadAll(resp.Body)
912+
defer resp2.Body.Close()
913+
if resp2.StatusCode != http.StatusOK {
914+
bodyBytes, _ := io.ReadAll(resp2.Body)
908915
t.Fatalf("Expected status 200, got %d, Response:%s", resp.StatusCode, string(bodyBytes))
909916
}
910-
resp.Body.Close()
911917

912918
// Request with protocol version not to be the one negotiated during initialization but also invalid/unsupported by server.
913-
req.Header.Set(headerKeyProtocolVersion, "2024-06-18")
914-
resp, err = server.Client().Do(req)
919+
req3, _ := http.NewRequest(http.MethodGet, server.URL, nil)
920+
req3.Header.Set("Content-Type", "text/event-stream")
921+
req3.Header.Set(headerKeyProtocolVersion, "2024-06-18")
922+
resp3, err := http.DefaultClient.Do(req3)
915923
if err != nil {
916924
t.Fatalf("Failed to send message: %v", err)
917925
}
918-
if resp.StatusCode != http.StatusBadRequest {
919-
t.Errorf("Expected status 400, got %d", resp.StatusCode)
926+
defer resp3.Body.Close()
927+
if resp3.StatusCode != http.StatusBadRequest {
928+
t.Errorf("Expected status 400, got %d", resp3.StatusCode)
920929
}
921-
resp.Body.Close()
922930
})
923931

924932
t.Run("DELETE Request", func(t *testing.T) {
925933
// Request with protocol version to be the one negotiated during initialization.
926-
req, _ := http.NewRequest(http.MethodDelete, server.URL, nil)
927-
req.Header.Set(headerKeyProtocolVersion, clientInitializedProtocolVersion)
928-
resp, err := http.DefaultClient.Do(req)
934+
req1, _ := http.NewRequest(http.MethodDelete, server.URL, nil)
935+
req1.Header.Set(headerKeyProtocolVersion, clientInitializedProtocolVersion)
936+
resp1, err := http.DefaultClient.Do(req1)
929937
if err != nil {
930938
t.Fatalf("Failed to send message: %v\n", err)
931939
}
932-
if resp.StatusCode != http.StatusOK {
933-
bodyBytes, _ := io.ReadAll(resp.Body)
934-
t.Fatalf("Expected status 200, got %d, Response:%s", resp.StatusCode, string(bodyBytes))
940+
defer resp1.Body.Close()
941+
if resp1.StatusCode != http.StatusOK {
942+
bodyBytes, _ := io.ReadAll(resp1.Body)
943+
t.Fatalf("Expected status 200, got %d, Response:%s", resp1.StatusCode, string(bodyBytes))
935944
}
936-
resp.Body.Close()
937945

938946
// Request with protocol version not to be the one negotiated during initialization but supported by server.
939-
req.Header.Set(headerKeyProtocolVersion, "2024-11-05")
940-
resp, err = http.DefaultClient.Do(req)
947+
req2, _ := http.NewRequest(http.MethodDelete, server.URL, nil)
948+
req2.Header.Set(headerKeyProtocolVersion, "2024-11-05")
949+
resp2, err := http.DefaultClient.Do(req2)
941950
if err != nil {
942951
t.Fatalf("Failed to send message: %v\n", err)
943952
}
944-
if resp.StatusCode != http.StatusOK {
945-
bodyBytes, _ := io.ReadAll(resp.Body)
946-
t.Fatalf("Expected status 200, got %d, Response:%s", resp.StatusCode, string(bodyBytes))
953+
defer resp2.Body.Close()
954+
if resp2.StatusCode != http.StatusOK {
955+
bodyBytes, _ := io.ReadAll(resp2.Body)
956+
t.Fatalf("Expected status 200, got %d, Response:%s", resp2.StatusCode, string(bodyBytes))
947957
}
948-
resp.Body.Close()
949958

950959
// Request with protocol version not to be the one negotiated during initialization but also invalid/unsupported by server.
951-
req.Header.Set(headerKeyProtocolVersion, "2024-06-18")
952-
resp, err = server.Client().Do(req)
960+
req3, _ := http.NewRequest(http.MethodDelete, server.URL, nil)
961+
req3.Header.Set(headerKeyProtocolVersion, "2024-06-18")
962+
resp3, err := http.DefaultClient.Do(req3)
953963
if err != nil {
954964
t.Fatalf("Failed to send message: %v", err)
955965
}
956-
if resp.StatusCode != http.StatusBadRequest {
957-
t.Errorf("Expected status 400, got %d", resp.StatusCode)
966+
defer resp3.Body.Close()
967+
if resp3.StatusCode != http.StatusBadRequest {
968+
t.Errorf("Expected status 400, got %d", resp3.StatusCode)
958969
}
959-
resp.Body.Close()
960970
})
961971
}
962972

0 commit comments

Comments
 (0)