MCPcopy
hub / github.com/keploy/keploy / TestMockReaderNDJSON

Function TestMockReaderNDJSON

pkg/platform/yaml/codec_test.go:291–349  ·  view source on GitHub ↗

TestMockReaderNDJSON tests reading NDJSON mock files.

(t *testing.T)

Source from the content-addressed store, hash-verified

289
290// TestMockReaderNDJSON tests reading NDJSON mock files.
291func TestMockReaderNDJSON(t *testing.T) {
292 ctx := context.Background()
293 logger := testLogger()
294
295 tempDir, err := os.MkdirTemp("", "mockreader_json_test")
296 if err != nil {
297 t.Fatalf("Failed to create temp dir: %v", err)
298 }
299 defer os.RemoveAll(tempDir)
300
301 // Build two mock docs and write as NDJSON
302 doc1 := buildHTTPDoc()
303 doc1.Name = "mock-0"
304 doc2 := buildHTTPDoc()
305 doc2.Name = "mock-1"
306
307 line1, err := MarshalDoc(FormatJSON, doc1)
308 if err != nil {
309 t.Fatalf("MarshalDoc 1: %v", err)
310 }
311 line2, err := MarshalDoc(FormatJSON, doc2)
312 if err != nil {
313 t.Fatalf("MarshalDoc 2: %v", err)
314 }
315
316 content := append(line1, '\n')
317 content = append(content, line2...)
318 content = append(content, '\n')
319
320 mockFile := filepath.Join(tempDir, "mocks.json")
321 if err := os.WriteFile(mockFile, content, 0644); err != nil {
322 t.Fatalf("Failed to write: %v", err)
323 }
324
325 reader, err := NewMockReaderF(ctx, logger, tempDir, "mocks", FormatJSON)
326 if err != nil {
327 t.Fatalf("NewMockReaderF: %v", err)
328 }
329 defer reader.Close()
330
331 names := []string{}
332 for {
333 doc, err := reader.ReadNextDoc()
334 if err == io.EOF {
335 break
336 }
337 if err != nil {
338 t.Fatalf("ReadNextDoc: %v", err)
339 }
340 names = append(names, doc.Name)
341 }
342
343 if len(names) != 2 {
344 t.Fatalf("got %d docs, want 2", len(names))
345 }
346 if names[0] != "mock-0" || names[1] != "mock-1" {
347 t.Errorf("names = %v, want [mock-0 mock-1]", names)
348 }

Callers

nothing calls this directly

Calls 6

buildHTTPDocFunction · 0.85
MarshalDocFunction · 0.85
NewMockReaderFFunction · 0.85
ReadNextDocMethod · 0.80
testLoggerFunction · 0.70
CloseMethod · 0.65

Tested by

no test coverage detected