|
| 1 | +/* |
| 2 | + * This file is part of arduino-cli. |
| 3 | + * |
| 4 | + * Copyright 2018 ARDUINO SA (http://www.arduino.cc/) |
| 5 | + * |
| 6 | + * This software is released under the GNU General Public License version 3, |
| 7 | + * which covers the main part of arduino-cli. |
| 8 | + * The terms of this license can be found at: |
| 9 | + * https://www.gnu.org/licenses/gpl-3.0.en.html |
| 10 | + * |
| 11 | + * You can be released from the requirements of the above licenses by purchasing |
| 12 | + * a commercial license. Buying such a license is mandatory if you want to modify or |
| 13 | + * otherwise use the software for commercial activities involving the Arduino |
| 14 | + * software without disclosing the source code of your own applications. To purchase |
| 15 | + * a commercial license, send an email to [email protected]. |
| 16 | + */ |
| 17 | + |
| 18 | +package resources |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/arduino/go-paths-helper" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +type EchoHandler struct{} |
| 33 | + |
| 34 | +// EchoHandler echos back the request as a response if used as http handler |
| 35 | +func (h *EchoHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { |
| 36 | + request.Write(writer) |
| 37 | +} |
| 38 | + |
| 39 | +func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) { |
| 40 | + goldUserAgentValue := fmt.Sprintf("arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019-06-12 11:11:11.111") |
| 41 | + goldUserAgentString := "User-Agent: " + goldUserAgentValue |
| 42 | + |
| 43 | + tmp, err := paths.MkTempDir("", "") |
| 44 | + require.NoError(t, err) |
| 45 | + defer tmp.RemoveAll() |
| 46 | + |
| 47 | + // startup echo server |
| 48 | + srv := httptest.NewServer(&EchoHandler{}) |
| 49 | + defer srv.Close() |
| 50 | + |
| 51 | + r := &DownloadResource{ |
| 52 | + ArchiveFileName: "echo.txt", |
| 53 | + CachePath: "cache", |
| 54 | + URL: srv.URL, |
| 55 | + } |
| 56 | + |
| 57 | + d, err := r.Download(tmp, http.Header{"User-Agent": []string{goldUserAgentValue}}) |
| 58 | + require.NoError(t, err) |
| 59 | + err = d.Run() |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + // leverage the download helper to download the echo for the request made by the downloader itself |
| 63 | + // |
| 64 | + // expect something like: |
| 65 | + // GET /echo HTTP/1.1 |
| 66 | + // Host: 127.0.0.1:64999 |
| 67 | + // User-Agent: arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019-06-12 11:11:11.111 |
| 68 | + // Accept-Encoding: gzip |
| 69 | + |
| 70 | + b, err := ioutil.ReadFile(tmp.String() + "/cache/echo.txt") // just pass the file name |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + requestLines := strings.Split(string(b), "\r\n") |
| 74 | + userAgentHeaderString := "" |
| 75 | + for _, line := range requestLines { |
| 76 | + if strings.Contains(line, "User-Agent: ") { |
| 77 | + userAgentHeaderString = line |
| 78 | + } |
| 79 | + } |
| 80 | + require.Equal(t, goldUserAgentString, userAgentHeaderString) |
| 81 | + |
| 82 | +} |
0 commit comments