bytes_test.go 2.62 KB
Newer Older
acud's avatar
acud committed
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package api_test

import (
	"bytes"
	"io/ioutil"
	"net/http"
	"testing"

13 14
	statestore "github.com/ethersphere/bee/pkg/statestore/mock"

acud's avatar
acud committed
15 16 17
	"github.com/ethersphere/bee/pkg/api"
	"github.com/ethersphere/bee/pkg/jsonhttp"
	"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
18
	"github.com/ethersphere/bee/pkg/logging"
acud's avatar
acud committed
19 20
	"github.com/ethersphere/bee/pkg/storage/mock"
	"github.com/ethersphere/bee/pkg/swarm"
21 22
	"github.com/ethersphere/bee/pkg/tags"
	mockbytes "gitlab.com/nolash/go-mockbytes"
acud's avatar
acud committed
23 24
)

25
// TestBytes tests that the data upload api responds as expected when uploading,
acud's avatar
acud committed
26
// downloading and requesting a resource that cannot be found.
27
func TestBytes(t *testing.T) {
acud's avatar
acud committed
28
	var (
29 30 31 32 33 34
		resource       = "/bytes"
		targets        = "0x222"
		expHash        = "29a5fb121ce96194ba8b7b823a1f9c6af87e1791f824940a53b5a7efe3f790d9"
		mockStorer     = mock.NewStorer()
		mockStatestore = statestore.NewStateStore()
		logger         = logging.New(ioutil.Discard, 0)
35
		client, _, _   = newTestServer(t, testServerOptions{
acud's avatar
acud committed
36
			Storer: mockStorer,
37
			Tags:   tags.NewTags(mockStatestore, logger),
38
			Logger: logging.New(ioutil.Discard, 5),
acud's avatar
acud committed
39 40
		})
	)
41 42 43 44 45
	g := mockbytes.New(0, mockbytes.MockTypeStandard).WithModulus(255)
	content, err := g.SequentialBytes(swarm.ChunkSize * 2)
	if err != nil {
		t.Fatal(err)
	}
acud's avatar
acud committed
46 47

	t.Run("upload", func(t *testing.T) {
48 49 50 51 52 53
		jsonhttptest.Request(t, client, http.MethodPost, resource, http.StatusOK,
			jsonhttptest.WithRequestBody(bytes.NewReader(content)),
			jsonhttptest.WithExpectedJSONResponse(api.BytesPostResponse{
				Reference: swarm.MustParseHexAddress(expHash),
			}),
		)
acud's avatar
acud committed
54 55 56
	})

	t.Run("download", func(t *testing.T) {
57
		resp := request(t, client, http.MethodGet, resource+"/"+expHash, nil, http.StatusOK)
acud's avatar
acud committed
58 59 60 61 62 63 64 65 66 67
		data, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			t.Fatal(err)
		}

		if !bytes.Equal(data, content) {
			t.Fatalf("data mismatch. got %s, want %s", string(data), string(content))
		}
	})

68 69 70 71 72 73 74 75
	t.Run("download-with-targets", func(t *testing.T) {
		resp := request(t, client, http.MethodGet, resource+"/"+expHash+"?targets="+targets, nil, http.StatusOK)

		if resp.Header.Get(api.TargetsRecoveryHeader) != targets {
			t.Fatalf("targets mismatch. got %s, want %s", resp.Header.Get(api.TargetsRecoveryHeader), targets)
		}
	})

acud's avatar
acud committed
76
	t.Run("not found", func(t *testing.T) {
77 78
		jsonhttptest.Request(t, client, http.MethodGet, resource+"/abcd", http.StatusNotFound,
			jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{
79
				Message: "Not Found",
80 81 82
				Code:    http.StatusNotFound,
			}),
		)
acud's avatar
acud committed
83 84
	})
}