github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/quality/elastic_search_transport_test.go (about) 1 /* 2 * Copyright (C) 2019 The "MysteriumNetwork/node" Authors. 3 * 4 * This program 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package quality 19 20 import ( 21 "bytes" 22 "fmt" 23 "net/http" 24 "net/http/httptest" 25 "runtime" 26 "testing" 27 "time" 28 29 "github.com/mysteriumnetwork/node/requests" 30 "github.com/stretchr/testify/assert" 31 ) 32 33 var httpClient = requests.NewHTTPClient("0.0.0.0", 1*time.Second) 34 35 func TestElasticSearchTransport_SendEvent_Success(t *testing.T) { 36 invoked := false 37 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 38 buffer := new(bytes.Buffer) 39 buffer.ReadFrom(r.Body) 40 body := buffer.String() 41 42 assert.JSONEq(t, `{ 43 "application": { 44 "name": "test app", 45 "version": "test version", 46 "os": "`+runtime.GOOS+`", 47 "arch": "`+runtime.GOARCH+`", 48 "launcher_version": "test version", 49 "host_os": "test os" 50 }, 51 "createdAt": 0, 52 "eventName": "", 53 "context": null 54 }`, body) 55 56 fmt.Fprint(w, "ok") 57 invoked = true 58 })) 59 60 app := appInfo{Name: "test app", Version: "test version", OS: runtime.GOOS, Arch: runtime.GOARCH, LauncherVersion: "test version", HostOS: "test os"} 61 event := Event{Application: app} 62 63 transport := NewElasticSearchTransport(httpClient, server.URL, time.Second) 64 transport.SendEvent(event) 65 66 assert.True(t, invoked) 67 } 68 69 func TestElasticSearchTransport_SendEvent_WithUnexpectedStatus(t *testing.T) { 70 invoked := false 71 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 72 w.WriteHeader(http.StatusInternalServerError) 73 fmt.Fprint(w, "something not cool happened") 74 invoked = true 75 })) 76 77 transport := NewElasticSearchTransport(httpClient, server.URL, time.Second) 78 transport.SendEvent(Event{}) 79 80 assert.True(t, invoked) 81 } 82 83 func TestElasticSearchTransport_SendEvent_WithUnexpectedResponse(t *testing.T) { 84 invoked := false 85 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 86 fmt.Fprint(w, "bad") 87 invoked = true 88 })) 89 90 transport := NewElasticSearchTransport(httpClient, server.URL, time.Second) 91 transport.SendEvent(Event{}) 92 93 assert.True(t, invoked) 94 }