Beispiel #1
0
func ExampleReader() {
	reader, err := warc.NewReader(os.Stdin)
	if err != nil {
		panic(err)
	}
	defer reader.Close()

	for {
		record, err := reader.ReadRecord()
		if err != nil {
			break
		}
		fmt.Println("Record:")
		for key, value := range record.Header {
			fmt.Printf("%v = %v\n", key, value)
		}
	}
}
Beispiel #2
0
func TestWriteRead(t *testing.T) {
	buffer := new(bytes.Buffer)

	writer := warc.NewWriter(buffer)
	for i, testRecord := range testRecords {
		t.Logf("writing record %d", i)
		record := warc.NewRecord()
		record.Header = testRecord.Header
		record.Content = bytes.NewReader(testRecord.Content)
		if _, err := writer.WriteRecord(record); err != nil {
			t.Fatal(err)
		}
	}

	reader, err := warc.NewReader(buffer)
	if err != nil {
		t.Fatalf("failed to create reader: %v", err)
	}
	for i, testRecord := range testRecords {
		t.Logf("reading record %d", i)
		record, err := reader.ReadRecord()
		if err != nil {
			t.Fatalf("expected record, got %v", err)
		}
		for key, val := range testRecord.Header {
			if record.Header[key] != val {
				t.Errorf("expected %q = %q, got %q", key, val, record.Header[key])
			}
		}
		content, err := ioutil.ReadAll(record.Content)
		if err != nil {
			t.Fatal(err)
		}
		if string(content) != string(testRecord.Content) {
			t.Errorf("expected %s = %s", content, testRecord.Content)
		}
	}
}