github.com/ethersphere/bee/v2@v2.2.0/pkg/api/welcome_message_test.go (about) 1 // Copyright 2020 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 "bytes" 9 "encoding/json" 10 "errors" 11 "net/http" 12 "testing" 13 14 "github.com/ethersphere/bee/v2/pkg/api" 15 "github.com/ethersphere/bee/v2/pkg/jsonhttp" 16 "github.com/ethersphere/bee/v2/pkg/jsonhttp/jsonhttptest" 17 "github.com/ethersphere/bee/v2/pkg/p2p/mock" 18 ) 19 20 func TestGetWelcomeMessage(t *testing.T) { 21 t.Parallel() 22 23 const DefaultTestWelcomeMessage = "Hello World!" 24 25 srv, _, _, _ := newTestServer(t, testServerOptions{ 26 P2P: mock.New(mock.WithGetWelcomeMessageFunc(func() string { 27 return DefaultTestWelcomeMessage 28 }))}) 29 30 jsonhttptest.Request(t, srv, http.MethodGet, "/welcome-message", http.StatusOK, 31 jsonhttptest.WithExpectedJSONResponse(api.WelcomeMessageResponse{ 32 WelcomeMesssage: DefaultTestWelcomeMessage, 33 }), 34 ) 35 } 36 37 // nolint:paralleltest 38 func TestSetWelcomeMessage(t *testing.T) { 39 testCases := []struct { 40 desc string 41 message string 42 wantFail bool 43 wantStatus int 44 wantMessage string 45 }{ 46 { 47 desc: "OK", 48 message: "Changed value", 49 wantStatus: http.StatusOK, 50 }, 51 { 52 desc: "OK - welcome message empty", 53 message: "", 54 wantStatus: http.StatusOK, 55 }, 56 { 57 desc: "error - request entity too large", 58 wantFail: true, 59 message: `zZZbzbzbzbBzBBZbBbZbbbBzzzZBZBbzzBBBbBzBzzZbbBzBBzBBbZz 60 bZZZBBbbZbbZzBbzBbzbZBZzBZZbZzZzZzbbZZBZzzbBZBzZzzBBzZZzzZbZZZzbbbzz 61 bBzZZBbBZBzZzBZBzbzBBbzBBzbzzzBbBbZzZBZBZzBZZbbZZBZZBzZzBZbzZBzZbBzZ 62 bbbBbbZzZbzbZzZzbzzzbzzbzZZzbbzbBZZbBbBZBBZzZzzbBBBBBZbZzBzzBbzBbbbz 63 BBzbbZBbzbzBZbzzBzbZBzzbzbbbBZBzBZzBZbzBzZzBZZZBzZZBzBZZzbzZbzzZzBBz 64 ZZzbZzzZZZBZBBbZZbZzBBBzbzZZbbZZBZZBBBbBZzZbZBZBBBzzZBbbbbzBzbbzBBBz 65 bZBBbZzBbZZBzbBbZZBzBzBzBBbzzzZBbzbZBbzBbZzbbBZBBbbZbBBbbBZbzbZzbBzB 66 bBbbZZbzZzbbBbzZbZZZZbzzZZbBzZZbZzZzzBzbZZ`, // 513 characters 67 wantStatus: http.StatusRequestEntityTooLarge, 68 }, 69 } 70 testURL := "/welcome-message" 71 72 for _, tC := range testCases { 73 t.Run(tC.desc, func(t *testing.T) { 74 mockP2P := mock.New() 75 76 srv, _, _, _ := newTestServer(t, testServerOptions{ 77 P2P: mockP2P, 78 }) 79 80 if tC.wantMessage == "" { 81 tC.wantMessage = http.StatusText(tC.wantStatus) 82 } 83 data, _ := json.Marshal(api.WelcomeMessageRequest{ 84 WelcomeMesssage: tC.message, 85 }) 86 body := bytes.NewReader(data) 87 wantResponse := jsonhttp.StatusResponse{ 88 Message: tC.wantMessage, 89 Code: tC.wantStatus, 90 } 91 jsonhttptest.Request(t, srv, http.MethodPost, testURL, tC.wantStatus, 92 jsonhttptest.WithRequestBody(body), 93 jsonhttptest.WithExpectedJSONResponse(wantResponse), 94 ) 95 if !tC.wantFail { 96 got := mockP2P.GetWelcomeMessage() 97 if got != tC.message { 98 t.Fatalf("could not set dynamic welcome message: want %s, got %s", tC.message, got) 99 } 100 } 101 }) 102 } 103 } 104 105 func TestSetWelcomeMessageInternalServerError(t *testing.T) { 106 t.Parallel() 107 108 testMessage := "NO CHANCE BYE" 109 testError := errors.New("Could not set value") 110 testURL := "/welcome-message" 111 112 srv, _, _, _ := newTestServer(t, testServerOptions{ 113 P2P: mock.New(mock.WithSetWelcomeMessageFunc(func(string) error { 114 return testError 115 })), 116 }) 117 118 data, _ := json.Marshal(api.WelcomeMessageRequest{ 119 WelcomeMesssage: testMessage, 120 }) 121 body := bytes.NewReader(data) 122 123 t.Run("internal server error - error on store", func(t *testing.T) { 124 t.Parallel() 125 126 wantCode := http.StatusInternalServerError 127 wantResp := jsonhttp.StatusResponse{ 128 Message: testError.Error(), 129 Code: wantCode, 130 } 131 jsonhttptest.Request(t, srv, http.MethodPost, testURL, wantCode, 132 jsonhttptest.WithRequestBody(body), 133 jsonhttptest.WithExpectedJSONResponse(wantResp), 134 ) 135 }) 136 }