16
16
package main
17
17
18
18
import (
19
+ "bytes"
19
20
"crypto/x509"
21
+ "encoding/json"
20
22
"encoding/pem"
23
+ "io"
24
+ "net/http"
25
+ "net/http/httptest"
21
26
"path/filepath"
22
27
"testing"
23
28
29
+ "github.com/arduino/arduino-create-agent/config"
30
+ "github.com/arduino/arduino-create-agent/gen/tools"
31
+ v2 "github.com/arduino/arduino-create-agent/v2"
32
+ "github.com/gin-gonic/gin"
24
33
"github.com/stretchr/testify/require"
25
34
)
26
35
@@ -38,3 +47,35 @@ func TestValidSignatureKey(t *testing.T) {
38
47
require .NoError (t , err )
39
48
require .NotNil (t , key )
40
49
}
50
+
51
+ func TestInstallToolDifferentContentType (t * testing.T ) {
52
+ r := gin .New ()
53
+ goa := v2 .Server (config .GetDataDir ().String ())
54
+ r .Any ("/v2/*path" , gin .WrapH (goa ))
55
+ ts := httptest .NewServer (r )
56
+
57
+ URL := "http://downloads.arduino.cc/tools/bossac-1.7.0-arduino3-linux64.tar.gz"
58
+ Checksum := "SHA-256:1ae54999c1f97234a5c603eb99ad39313b11746a4ca517269a9285afa05f9100"
59
+ request := tools.ToolPayload {
60
+ Name : "bossac" ,
61
+ Version : "1.7.0-arduino3" ,
62
+ Packager : "arduino" ,
63
+ URL : & URL ,
64
+ Checksum : & Checksum ,
65
+ }
66
+
67
+ payload , err := json .Marshal (request )
68
+ require .NoError (t , err )
69
+
70
+ // for some reason the fronted sends requests with "text/plain" content type.
71
+ // Even if the request body contains a json object.
72
+ // With this test we verify is parsed correctly.
73
+ for _ , encoding := range []string {"encoding/json" , "text/plain" } {
74
+ resp , err := http .Post (ts .URL + "/v2/pkgs/tools/installed" , encoding , bytes .NewBuffer (payload ))
75
+ require .NoError (t , err )
76
+ body , err := io .ReadAll (resp .Body )
77
+ require .NoError (t , err )
78
+ require .Contains (t , string (body ), "ok" )
79
+ require .Equal (t , http .StatusOK , resp .StatusCode )
80
+ }
81
+ }
0 commit comments