Example #1
0
func TestModifyIndex(t *testing.T) {
	offset := 1
	query := url.Values{
		"objecthash": []string{""},
		"blocktype":  []string{swarm.BLOCK_TYPE_INDEX},
		"baseobject": []string{TestIndexhash},
		"offset":     []string{swarm.Itoa(offset)},
	}

	body := TestFile[offset:]
	pdata, err := RequestHttp(TestServer, swarm.OSS_PORT, swarm.URL_SWARM_WRITE_OSS, query, "POST", &body, false, swarm.WRITE_BLOCK_TIMEOUT, int64(len(body)))
	if err != nil {
		t.Errorf(err.Error())
		return
	}
	fmt.Println(string(*pdata))

	// parse response
	var response swarm.ObjectWriteResponse
	if err = json.Unmarshal(*pdata, &response); err != nil {
		t.Errorf("Decode error: %s", err.Error())
		return
	}

	// check status
	if response.Status != swarm.STATUS_OK || response.Objecthash != TestIndexhash {
		t.Errorf("Failed TestModifyIndex")
		return
	}
}
Example #2
0
func TestReadObject(t *testing.T) {
	offset := 1
	query := url.Values{
		"objecthash": []string{TestObjecthash},
		"blocktype":  []string{swarm.BLOCK_TYPE_OBJECT},
		"offset":     []string{swarm.Itoa(offset)},
	}

	pdata, err := RequestHttp(TestServer, swarm.OSS_PORT, swarm.URL_SWARM_READ_OSS, query, "GET", nil, false, swarm.READ_BLOCK_TIMEOUT, 0)
	if err != nil {
		t.Errorf(err.Error())
		return
	}
	fmt.Println(string(*pdata))

	if !bytes.Equal([]byte(TestFile[offset:]), *pdata) {
		t.Errorf("Mismatch TestReadObject: %v", *pdata)
	}
}
Example #3
0
// Read object from swarm
func readObject(objecthash string, swarmServers string, offset int64) (body []byte, err error) {
	v := url.Values{}
	v.Set("objecthash", objecthash)
	v.Set("blocktype", swarm.BLOCK_TYPE_INDEX)
	v.Set("offset", swarm.Itoa(offset))

	u := url.URL{
		Scheme:   "http",
		Host:     swarmServers + ":" + swarm.OSS_PORT,
		Path:     swarm.URL_SWARM_READ_OSS,
		RawQuery: v.Encode(),
	}

	// prepare request
	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		logger.Error("Read %s error %s", objecthash, err.Error())
		return
	}

	client := &http.Client{}

	resp, err := client.Do(req)
	if err != nil {
		logger.Error("Read %s error %s", objecthash, err.Error())
		return
	}
	if resp.Status != "200 OK" {
		logger.Error("Read %s error status %s", objecthash, resp.Status)
		return
	}

	// var buf [512]byte
	reader := resp.Body
	defer reader.Close()
	body, err = ioutil.ReadAll(reader)
	if err != nil {
		logger.Error("Read %s error %s", objecthash, err.Error())
		return
	}

	return body, nil
}
// Do http request
func RequestHttp(nodeIp string, port string, path string, query url.Values,
	method string, body *[]byte, verifyJsonResponse bool, timeout time.Duration, contentLength int64) (pdata *[]byte, err error) {

	u := url.URL{
		Scheme:   "http",
		Host:     nodeIp + ":" + port,
		Path:     path,
		RawQuery: query.Encode(),
	}

	// prepare request
	buff := bytes.NewBuffer([]byte(""))
	if body != nil {
		buff = bytes.NewBuffer(*body)
	}
	req, err := http.NewRequest(method, u.String(), buff)
	if method == "POST" && contentLength != 0 {
		req.Header.Add("Content-Length", swarm.Itoa(contentLength))
	}
	if err != nil {
		return
	}

	respChan := make(chan swarm.HttpResponse)

	go func() {
		result := swarm.HttpResponse{}

		client := &http.Client{}

		resp, err := client.Do(req)
		if err != nil {
			result.Error = err
			respChan <- result
			return
		}
		defer resp.Body.Close()

		if resp.Status != "200 OK" {
			msg := fmt.Sprintf("node %s resp status: %s", nodeIp, resp.Status)
			result.Error = fmt.Errorf(msg)
			respChan <- result
			return
		}

		_temp, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			result.Error = err
			respChan <- result
			return
		}

		result.Response = &_temp
		respChan <- result
	}()

	// blocking wait
	select {
	case result := <-respChan:
		if result.Error != nil {
			err = result.Error
			return
		}
		pdata = result.Response
	case <-time.After(timeout):
		err = fmt.Errorf("Timeout: http://%s:%s%s", nodeIp, port, path)
		return
	}

	if verifyJsonResponse {
		var response swarm.BaseResponse
		// parse response
		if err = json.Unmarshal(*pdata, &response); err != nil {
			return
		}

		// check status
		if response.Status != swarm.STATUS_OK {
			msg := fmt.Sprintf("node %s, error: %s", nodeIp, err.Error())
			err = fmt.Errorf(msg)
			return
		}
		return pdata, nil
	}
	return pdata, nil
}