|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 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-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 httpclient |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "io/ioutil" |
| 21 | + "net/http" |
| 22 | + "net/http/httptest" |
| 23 | + "net/url" |
| 24 | + "testing" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +func TestUserAgentHeader(t *testing.T) { |
| 30 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 31 | + fmt.Fprint(w, r.Header.Get("User-Agent")) |
| 32 | + })) |
| 33 | + defer ts.Close() |
| 34 | + |
| 35 | + client := NewWithConfig(&Config{ |
| 36 | + UserAgent: "test-user-agent", |
| 37 | + }) |
| 38 | + |
| 39 | + request, err := http.NewRequest("GET", ts.URL, nil) |
| 40 | + require.NoError(t, err) |
| 41 | + |
| 42 | + response, err := client.Do(request) |
| 43 | + require.NoError(t, err) |
| 44 | + |
| 45 | + b, err := ioutil.ReadAll(response.Body) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + require.Equal(t, "test-user-agent", string(b)) |
| 49 | +} |
| 50 | + |
| 51 | +func TestProxy(t *testing.T) { |
| 52 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 53 | + w.WriteHeader(http.StatusNoContent) |
| 54 | + })) |
| 55 | + defer ts.Close() |
| 56 | + |
| 57 | + proxyURL, err := url.Parse(ts.URL) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + client := NewWithConfig(&Config{ |
| 61 | + Proxy: proxyURL, |
| 62 | + }) |
| 63 | + |
| 64 | + request, err := http.NewRequest("GET", "http://arduino.cc", nil) |
| 65 | + require.NoError(t, err) |
| 66 | + |
| 67 | + response, err := client.Do(request) |
| 68 | + require.NoError(t, err) |
| 69 | + require.Equal(t, http.StatusNoContent, response.StatusCode) |
| 70 | +} |
0 commit comments