github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/api/storage_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:43</date> 10 //</624450112411668480> 11 12 13 package api 14 15 import ( 16 "context" 17 "testing" 18 ) 19 20 func testStorage(t *testing.T, f func(*Storage, bool)) { 21 testAPI(t, func(api *API, toEncrypt bool) { 22 f(NewStorage(api), toEncrypt) 23 }) 24 } 25 26 func TestStoragePutGet(t *testing.T) { 27 testStorage(t, func(api *Storage, toEncrypt bool) { 28 content := "hello" 29 exp := expResponse(content, "text/plain", 0) 30 //exp:=expresponse([]byte(content),“text/plain”,0) 31 ctx := context.TODO() 32 bzzkey, wait, err := api.Put(ctx, content, exp.MimeType, toEncrypt) 33 if err != nil { 34 t.Fatalf("unexpected error: %v", err) 35 } 36 err = wait(ctx) 37 if err != nil { 38 t.Fatalf("unexpected error: %v", err) 39 } 40 bzzhash := bzzkey.Hex() 41 //根据API检查Put 42 resp0 := testGet(t, api.api, bzzhash, "") 43 checkResponse(t, resp0, exp) 44 45 //检查存储获取 46 resp, err := api.Get(context.TODO(), bzzhash) 47 if err != nil { 48 t.Fatalf("unexpected error: %v", err) 49 } 50 checkResponse(t, &testResponse{nil, resp}, exp) 51 }) 52 } 53