-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
Copy pathlist_test.go
93 lines (81 loc) · 2.63 KB
/
list_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// This file is part of arduino-cli.
//
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].
package board
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetByVidPid(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `
{
"architecture": "samd",
"fqbn": "arduino:samd:mkr1000",
"href": "/v3/boards/arduino:samd:mkr1000",
"id": "mkr1000",
"name": "Arduino/Genuino MKR1000",
"package": "arduino",
"plan": "create-free"
}
`)
}))
defer ts.Close()
vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069")
require.Nil(t, err)
require.Len(t, res, 1)
require.Equal(t, "Arduino/Genuino MKR1000", res[0].Name)
require.Equal(t, "arduino:samd:mkr1000", res[0].FQBN)
// wrong vid/pid
res, err = apiByVidPid("foo", "")
require.NotNil(t, err)
}
func TestGetByVidPidNotFound(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer ts.Close()
vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069")
require.NotNil(t, err)
require.Equal(t, "board not found", err.Error())
require.Len(t, res, 0)
}
func TestGetByVidPid5xx(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Ooooops!"))
}))
defer ts.Close()
vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069")
require.NotNil(t, err)
require.Equal(t, "the server responded with status 500 Internal Server Error", err.Error())
require.Len(t, res, 0)
}
func TestGetByVidPidMalformedResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{}")
}))
defer ts.Close()
vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069")
require.NotNil(t, err)
require.Equal(t, "wrong format in server response", err.Error())
require.Len(t, res, 0)
}