Similarityoung commented on code in PR #674:
URL: https://github.com/apache/dubbo-go-pixiu/pull/674#discussion_r2076854964


##########
pkg/common/http/manager_test.go:
##########
@@ -278,3 +279,221 @@ func NewTestServerWithURL(URL string, handler 
http.Handler) (*httptest.Server, e
        ts.Start()
        return ts, nil
 }
+
+// StreamHTTPRecorder Used to capture and test streaming HTTP responses over 
channels
+type StreamHTTPRecorder struct {
+       http.ResponseWriter
+       receivedBuf []string
+       headers     http.Header
+       status      int
+       flushCount  int
+}
+
+func NewStreamHTTPRecorder() *StreamHTTPRecorder {
+       return &StreamHTTPRecorder{
+               receivedBuf: make([]string, 0),
+               headers:     make(http.Header),
+               flushCount:  0,
+       }
+}
+
+func (r *StreamHTTPRecorder) Header() http.Header {
+       return r.headers
+}
+
+func (r *StreamHTTPRecorder) WriteHeader(statusCode int) {
+       r.status = statusCode
+}
+
+func (r *StreamHTTPRecorder) Write(data []byte) (int, error) {
+       eventCh <- string(data)
+       r.receivedBuf = append(r.receivedBuf, string(data))
+       return len(data), nil
+}
+
+func (r *StreamHTTPRecorder) Flush() {
+       r.flushCount++
+}
+
+// Test a variety of common streaming HTTP response types
+func TestStreamableHTTPResponse(t *testing.T) {
+       // define the type of content you want to test
+       contentTypes := []string{
+               "text/plain",
+               "application/json",
+               "application/octet-stream",
+               "application/x-ndjson",
+       }
+
+       for _, contentType := range contentTypes {
+               t.Run(fmt.Sprintf("ContentType_%s", contentType), func(t 
*testing.T) {
+                       testStreamableResponse(t, contentType)
+               })
+       }
+}
+
+func testStreamableResponse(t *testing.T, contentType string) {
+       hcmc := model.HttpConnectionManagerConfig{
+               RouteConfig: model.RouteConfiguration{
+                       RouteTrie: trie.NewTrieWithDefault("GET/api/stream", 
model.RouteAction{
+                               Cluster: "mock_stream_cluster",
+                       }),
+               },
+               HTTPFilters: []*model.HTTPFilter{
+                       {
+                               Name: commonmock.Kind,
+                       },
+               },
+       }
+
+       ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
+       defer cancel()
+
+       // Clear any data that may have been left over from the previous test
+       for len(eventCh) > 0 {
+               <-eventCh
+       }
+
+       // mock server
+       upstreamServer, _ := NewTestServerWithURL("localhost:8080", 
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               w.Header().Set("Content-Type", contentType)
+               flusher := w.(http.Flusher)
+
+               // Generate appropriate test data based on content type
+               var data []byte
+               for i := 1; i <= 5; i++ {
+                       select {
+                       case <-ctx.Done():
+                               return
+                       default:
+                               time.Sleep(10 * time.Millisecond)
+
+                               switch contentType {
+                               case "application/json":

Review Comment:
   👌



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to