예제 #1
0
func ExampleWriter() {
	writer := warc.NewWriter(os.Stdout)
	record := warc.NewRecord()
	record.Header.Set("warc-type", "resource")
	record.Header.Set("content-type", "plain/text")
	record.Content = strings.NewReader("Hello, World!")
	if _, err := writer.WriteRecord(record); err != nil {
		panic(err)
	}
}
예제 #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)
		}
	}
}