TestEncodeDocToJSONStreaming verifies EncodeDocTo writes NDJSON-compatible output: one compact JSON object followed by '\n', round-trippable via UnmarshalDoc(FormatJSON).
(t *testing.T)
| 240 | // output: one compact JSON object followed by '\n', round-trippable via |
| 241 | // UnmarshalDoc(FormatJSON). |
| 242 | func TestEncodeDocToJSONStreaming(t *testing.T) { |
| 243 | doc := buildHTTPDoc() |
| 244 | |
| 245 | var buf bytes.Buffer |
| 246 | if err := EncodeDocTo(&buf, FormatJSON, doc); err != nil { |
| 247 | t.Fatalf("EncodeDocTo(JSON): %v", err) |
| 248 | } |
| 249 | |
| 250 | out := buf.Bytes() |
| 251 | if len(out) == 0 || out[len(out)-1] != '\n' { |
| 252 | t.Fatalf("JSON stream should end with '\\n' for NDJSON; got %q", out) |
| 253 | } |
| 254 | // A single-document NDJSON stream has exactly one newline at the end. |
| 255 | if nl := bytes.Count(out, []byte{'\n'}); nl != 1 { |
| 256 | t.Errorf("expected 1 newline in single-doc NDJSON, got %d", nl) |
| 257 | } |
| 258 | if !json.Valid(bytes.TrimRight(out, "\n")) { |
| 259 | t.Fatalf("payload is not valid JSON: %s", out) |
| 260 | } |
| 261 | |
| 262 | decoded, err := UnmarshalDoc(FormatJSON, bytes.TrimRight(out, "\n")) |
| 263 | if err != nil { |
| 264 | t.Fatalf("UnmarshalDoc: %v", err) |
| 265 | } |
| 266 | if decoded.Name != "test-1" || decoded.Kind != models.HTTP { |
| 267 | t.Errorf("decoded doc = %+v", decoded) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // TestEncodeDocToYAMLStreaming verifies EncodeDocTo writes a single YAML |
| 272 | // document that re-parses cleanly. |
nothing calls this directly
no test coverage detected