github.com/ethersphere/bee/v2@v2.2.0/pkg/api/subdomain_test.go (about)

     1  // Copyright 2022 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api_test
     6  
     7  import (
     8  	"fmt"
     9  	"net/http"
    10  	"path"
    11  	"testing"
    12  
    13  	"github.com/ethersphere/bee/v2/pkg/api"
    14  	"github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest"
    15  	"github.com/ethersphere/bee/v2/pkg/log"
    16  	mockpost "github.com/ethersphere/bee/v2/pkg/postage/mock"
    17  	resolverMock "github.com/ethersphere/bee/v2/pkg/resolver/mock"
    18  	mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock"
    19  	"github.com/ethersphere/bee/v2/pkg/swarm"
    20  )
    21  
    22  func TestSubdomains(t *testing.T) {
    23  	t.Parallel()
    24  
    25  	for _, tc := range []struct {
    26  		name                string
    27  		files               []f
    28  		expectedReference   swarm.Address
    29  		wantIndexFilename   string
    30  		wantErrorFilename   string
    31  		indexFilenameOption jsonhttptest.Option
    32  		errorFilenameOption jsonhttptest.Option
    33  	}{
    34  		{
    35  			name:              "nested files with extension",
    36  			expectedReference: swarm.MustParseHexAddress("4c9c76d63856102e54092c38a7cd227d769752d768b7adc8c3542e3dd9fcf295"),
    37  			files: []f{
    38  				{
    39  					data: []byte("robots text"),
    40  					name: "robots.txt",
    41  					dir:  "",
    42  					header: http.Header{
    43  						api.ContentTypeHeader: {"text/plain; charset=utf-8"},
    44  					},
    45  				},
    46  				{
    47  					data: []byte("image 1"),
    48  					name: "1.png",
    49  					dir:  "img",
    50  					header: http.Header{
    51  						api.ContentTypeHeader: {"image/png"},
    52  					},
    53  				},
    54  				{
    55  					data: []byte("image 2"),
    56  					name: "2.png",
    57  					dir:  "img",
    58  					header: http.Header{
    59  						api.ContentTypeHeader: {"image/png"},
    60  					},
    61  				},
    62  			},
    63  		},
    64  		{
    65  			name:                "explicit index and error filename",
    66  			expectedReference:   swarm.MustParseHexAddress("2cd9a6ac11eefbb71b372fb97c3ef64109c409955964a294fdc183c1014b3844"),
    67  			wantIndexFilename:   "index.html",
    68  			wantErrorFilename:   "error.html",
    69  			indexFilenameOption: jsonhttptest.WithRequestHeader(api.SwarmIndexDocumentHeader, "index.html"),
    70  			errorFilenameOption: jsonhttptest.WithRequestHeader(api.SwarmErrorDocumentHeader, "error.html"),
    71  			files: []f{
    72  				{
    73  					data: []byte("<h1>Swarm"),
    74  					name: "index.html",
    75  					dir:  "",
    76  					header: http.Header{
    77  						api.ContentTypeHeader: {"text/html; charset=utf-8"},
    78  					},
    79  				},
    80  				{
    81  					data: []byte("<h2>404"),
    82  					name: "error.html",
    83  					dir:  "",
    84  					header: http.Header{
    85  						api.ContentTypeHeader: {"text/html; charset=utf-8"},
    86  					},
    87  				},
    88  			},
    89  		},
    90  	} {
    91  		tc := tc
    92  		t.Run(tc.name, func(t *testing.T) {
    93  			t.Parallel()
    94  
    95  			var (
    96  				dirUploadResource = "/bzz"
    97  				storer            = mockstorer.New()
    98  				logger            = log.Noop
    99  				client, _, _, _   = newTestServer(t, testServerOptions{
   100  					Storer:          storer,
   101  					Logger:          logger,
   102  					PreventRedirect: true,
   103  					Post:            mockpost.New(mockpost.WithAcceptAll()),
   104  					Resolver: resolverMock.NewResolver(
   105  						resolverMock.WithResolveFunc(
   106  							func(string) (swarm.Address, error) {
   107  								return tc.expectedReference, nil
   108  							},
   109  						),
   110  					),
   111  				})
   112  			)
   113  
   114  			validateAltPath := func(t *testing.T, fromPath, toPath string) {
   115  				t.Helper()
   116  
   117  				var respBytes []byte
   118  
   119  				jsonhttptest.Request(t, client, http.MethodGet,
   120  					fmt.Sprintf("http://test.eth.swarm.localhost/%s", toPath), http.StatusOK,
   121  					jsonhttptest.WithPutResponseBody(&respBytes),
   122  				)
   123  
   124  				jsonhttptest.Request(t, client, http.MethodGet,
   125  					fmt.Sprintf("http://test.eth.swarm.localhost/%s", fromPath), http.StatusOK,
   126  					jsonhttptest.WithExpectedResponse(respBytes),
   127  				)
   128  			}
   129  
   130  			tarReader := tarFiles(t, tc.files)
   131  
   132  			var resp api.BzzUploadResponse
   133  
   134  			options := []jsonhttptest.Option{
   135  				jsonhttptest.WithRequestHeader(api.SwarmDeferredUploadHeader, "true"),
   136  				jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr),
   137  				jsonhttptest.WithRequestBody(tarReader),
   138  				jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"),
   139  				jsonhttptest.WithRequestHeader(api.ContentTypeHeader, api.ContentTypeTar),
   140  				jsonhttptest.WithUnmarshalJSONResponse(&resp),
   141  			}
   142  			if tc.indexFilenameOption != nil {
   143  				options = append(options, tc.indexFilenameOption)
   144  			}
   145  			if tc.errorFilenameOption != nil {
   146  				options = append(options, tc.errorFilenameOption)
   147  			}
   148  
   149  			jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusCreated, options...)
   150  
   151  			if resp.Reference.String() == "" {
   152  				t.Fatalf("expected file reference, did not got any")
   153  			}
   154  
   155  			if tc.expectedReference.String() != resp.Reference.String() {
   156  				t.Fatalf("got unexpected reference exp %s got %s", tc.expectedReference.String(), resp.Reference.String())
   157  			}
   158  
   159  			for _, f := range tc.files {
   160  				jsonhttptest.Request(
   161  					t, client, http.MethodGet,
   162  					fmt.Sprintf("http://test.eth.swarm.localhost/%s", path.Join(f.dir, f.name)),
   163  					http.StatusOK,
   164  					jsonhttptest.WithExpectedResponse(f.data),
   165  				)
   166  			}
   167  
   168  			if tc.wantIndexFilename != "" {
   169  				validateAltPath(t, "", tc.wantIndexFilename)
   170  			}
   171  			if tc.wantErrorFilename != "" {
   172  				validateAltPath(t, "_non_existent_file_path_", tc.wantErrorFilename)
   173  			}
   174  		})
   175  	}
   176  }