github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/cmd/swarm/upload_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 //"io" 21 "io/ioutil" 22 "net/http" 23 //"os" 24 "testing" 25 ) 26 27 //remove it temp 28 // TestCLISwarmUp tests that running 'swarm up' makes the resulting file 29 // available from all nodes via the HTTP API 30 //func TestCLISwarmUp(t *testing.T) { 31 // // start 3 node cluster 32 // t.Log("starting 3 node cluster") 33 // cluster := newTestCluster(t, 3) 34 // defer cluster.Shutdown() 35 // 36 // // create a tmp file 37 // tmp, err := ioutil.TempFile("", "swarm-test") 38 // assertNil(t, err) 39 // defer tmp.Close() 40 // defer os.Remove(tmp.Name()) 41 // _, err = io.WriteString(tmp, "data") 42 // assertNil(t, err) 43 // 44 // // upload the file with 'swarm up' and expect a hash 45 // t.Log("uploading file with 'swarm up'") 46 // up := runSwarm(t, "--bzzapi", cluster.Nodes[0].URL, "up", tmp.Name()) 47 // _, matches := up.ExpectRegexp(`[a-f\d]{64}`) 48 // up.ExpectExit() 49 // hash := matches[0] 50 // t.Logf("file uploaded with hash %s", hash) 51 // 52 // // get the file from the HTTP API of each node 53 // for _, node := range cluster.Nodes { 54 // t.Logf("getting file from %s", node.Name) 55 // res, err := http.Get(node.URL + "/bzz:/" + hash) 56 // assertNil(t, err) 57 // assertHTTPResponse(t, res, http.StatusOK, "data") 58 // } 59 //} 60 61 func assertNil(t *testing.T, err error) { 62 if err != nil { 63 t.Fatal(err) 64 } 65 } 66 67 func assertHTTPResponse(t *testing.T, res *http.Response, expectedStatus int, expectedBody string) { 68 defer res.Body.Close() 69 if res.StatusCode != expectedStatus { 70 t.Fatalf("expected HTTP status %d, got %s", expectedStatus, res.Status) 71 } 72 data, err := ioutil.ReadAll(res.Body) 73 assertNil(t, err) 74 if string(data) != expectedBody { 75 t.Fatalf("expected HTTP body %q, got %q", expectedBody, data) 76 } 77 }