1616package main
1717
1818import (
19+ "bytes"
1920 "crypto/x509"
21+ "encoding/json"
2022 "encoding/pem"
23+ "io"
24+ "net/http"
25+ "net/http/httptest"
2126 "path/filepath"
2227 "testing"
2328
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"
2433 "github.com/stretchr/testify/require"
2534)
2635
@@ -38,3 +47,35 @@ func TestValidSignatureKey(t *testing.T) {
3847 require .NoError (t , err )
3948 require .NotNil (t , key )
4049}
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