| 91 | } |
| 92 | |
| 93 | func TestStreamWriteYAMLResponse(t *testing.T) { |
| 94 | type testStruct struct { |
| 95 | Name string `yaml:"name"` |
| 96 | Value int `yaml:"value"` |
| 97 | } |
| 98 | tt := struct { |
| 99 | name string |
| 100 | headers map[string]string |
| 101 | expectedOutput string |
| 102 | expectedContentType string |
| 103 | value map[string]*testStruct |
| 104 | }{ |
| 105 | name: "Test Stream Render YAML", |
| 106 | headers: map[string]string{ |
| 107 | "Content-Type": "application/yaml", |
| 108 | }, |
| 109 | expectedContentType: "application/yaml", |
| 110 | value: make(map[string]*testStruct), |
| 111 | } |
| 112 | |
| 113 | // Generate some data to serialize. |
| 114 | for i := 0; i < rand.Intn(100)+1; i++ { |
| 115 | ts := testStruct{ |
| 116 | Name: "testName" + strconv.Itoa(i), |
| 117 | Value: i, |
| 118 | } |
| 119 | tt.value[ts.Name] = &ts |
| 120 | } |
| 121 | d, err := yaml.Marshal(tt.value) |
| 122 | require.NoError(t, err) |
| 123 | tt.expectedOutput = string(d) |
| 124 | w := httptest.NewRecorder() |
| 125 | |
| 126 | done := make(chan struct{}) |
| 127 | iter := make(chan any) |
| 128 | go func() { |
| 129 | util.StreamWriteYAMLResponse(w, iter, util_log.Logger) |
| 130 | close(done) |
| 131 | }() |
| 132 | for k, v := range tt.value { |
| 133 | iter <- map[string]*testStruct{k: v} |
| 134 | } |
| 135 | close(iter) |
| 136 | <-done |
| 137 | assert.Equal(t, tt.expectedContentType, w.Header().Get("Content-Type")) |
| 138 | assert.Equal(t, 200, w.Code) |
| 139 | assert.YAMLEq(t, tt.expectedOutput, w.Body.String()) |
| 140 | } |
| 141 | |
| 142 | func TestParseProtoReader(t *testing.T) { |
| 143 | // 47 bytes compressed and 53 uncompressed |