|
| 1 | +// This file is part of arduino-app-cli. |
| 2 | +// |
| 3 | +// Copyright 2025 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-app-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package version |
| 17 | + |
| 18 | +import ( |
| 19 | + "errors" |
| 20 | + "io" |
| 21 | + "net/http" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestDaemonVersion(t *testing.T) { |
| 29 | + testCases := []struct { |
| 30 | + name string |
| 31 | + serverStub Tripper |
| 32 | + port string |
| 33 | + expectedResult string |
| 34 | + expectedErrorMessage string |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "return the server version when the server is up", |
| 38 | + serverStub: successServer, |
| 39 | + port: "8800", |
| 40 | + expectedResult: "3.0-server", |
| 41 | + expectedErrorMessage: "", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "return error if default server is not listening on default port", |
| 45 | + serverStub: failureServer, |
| 46 | + port: "8800", |
| 47 | + expectedResult: "", |
| 48 | + expectedErrorMessage: `Get "http://localhost:8800/v1/version": connection refused`, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "return error if provided server is not listening on provided port", |
| 52 | + serverStub: failureServer, |
| 53 | + port: "1234", |
| 54 | + expectedResult: "", |
| 55 | + expectedErrorMessage: `Get "http://localhost:1234/v1/version": connection refused`, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "return error for server response 500 Internal Server Error", |
| 59 | + serverStub: failureInternalServerError, |
| 60 | + port: "0", |
| 61 | + expectedResult: "", |
| 62 | + expectedErrorMessage: "unexpected status code received", |
| 63 | + }, |
| 64 | + |
| 65 | + { |
| 66 | + name: "return error for server up and wrong json response", |
| 67 | + serverStub: successServerWrongJson, |
| 68 | + port: "8800", |
| 69 | + expectedResult: "", |
| 70 | + expectedErrorMessage: "invalid character '<' looking for beginning of value", |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tc := range testCases { |
| 75 | + t.Run(tc.name, func(t *testing.T) { |
| 76 | + // arrange |
| 77 | + httpClient := http.Client{} |
| 78 | + httpClient.Transport = tc.serverStub |
| 79 | + |
| 80 | + // act |
| 81 | + result, err := getDaemonVersion(httpClient, tc.port) |
| 82 | + |
| 83 | + // assert |
| 84 | + require.Equal(t, tc.expectedResult, result) |
| 85 | + if err != nil { |
| 86 | + require.Equal(t, tc.expectedErrorMessage, err.Error()) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Leverage the http.Client's RoundTripper |
| 93 | +// to return a canned response and bypass network calls. |
| 94 | +type Tripper func(*http.Request) (*http.Response, error) |
| 95 | + |
| 96 | +func (t Tripper) RoundTrip(request *http.Request) (*http.Response, error) { |
| 97 | + return t(request) |
| 98 | +} |
| 99 | + |
| 100 | +var successServer = Tripper(func(*http.Request) (*http.Response, error) { |
| 101 | + body := io.NopCloser(strings.NewReader(`{"version":"3.0-server"}`)) |
| 102 | + return &http.Response{ |
| 103 | + StatusCode: http.StatusOK, |
| 104 | + Body: body, |
| 105 | + }, nil |
| 106 | +}) |
| 107 | + |
| 108 | +var successServerWrongJson = Tripper(func(*http.Request) (*http.Response, error) { |
| 109 | + body := io.NopCloser(strings.NewReader(`<!doctype html><html lang="en"`)) |
| 110 | + return &http.Response{ |
| 111 | + StatusCode: http.StatusOK, |
| 112 | + Body: body, |
| 113 | + }, nil |
| 114 | +}) |
| 115 | + |
| 116 | +var failureServer = Tripper(func(*http.Request) (*http.Response, error) { |
| 117 | + return nil, errors.New("connection refused") |
| 118 | +}) |
| 119 | + |
| 120 | +var failureInternalServerError = Tripper(func(*http.Request) (*http.Response, error) { |
| 121 | + return &http.Response{ |
| 122 | + StatusCode: http.StatusInternalServerError, |
| 123 | + Body: io.NopCloser(strings.NewReader("")), |
| 124 | + }, nil |
| 125 | +}) |
0 commit comments