示例#1
0
文件: main.go 项目: schorlet/cdc
func openEntry(cache *cdc.DiskCache, url, addr, dir string) *cdc.Entry {
	var entry *cdc.Entry
	var err error

	if addr != "" {
		id, era := strconv.ParseUint(addr, 10, 32)
		if era != nil {
			log.Fatal(era)
		}
		entry, err = cdc.OpenEntry(cdc.CacheAddr(id), dir)

	} else if url != "" {
		entry, err = cache.OpenURL(url)
	}

	if err != nil {
		log.Fatal(err)
	}

	return entry
}
示例#2
0
文件: cdc_test.go 项目: schorlet/cdc
func TestEntry(t *testing.T) {
	addr := cdc.CacheAddr(2684420101)
	entry, err := cdc.OpenEntry(addr, "testcache")
	if err != nil {
		t.Fatal(err)
	}

	url := "https://golang.org/doc/gopher/pkg.png"
	if url != entry.URL() {
		t.Fatalf("got: %s, want: %s", entry.URL(), url)
	}

	header, err := entry.Header()
	if err != nil {
		t.Fatal(err)
	}

	cl := header.Get("Content-Length")
	if cl != "5409" {
		t.Fatalf("got: %s, want: 5409", cl)
	}

	ct := header.Get("Content-Type")
	if ct != "image/png" {
		t.Fatalf("got: %s, want: image/png", ct)
	}

	body, err := entry.Body()
	if err != nil {
		t.Fatal(err)
	}
	n, err := io.Copy(ioutil.Discard, body)
	if err != nil {
		t.Fatal(err)
	}
	if n != 5409 {
		t.Fatalf("got: %d, want: 5409", n)
	}
	body.Close()
}