github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/configstate/proxyconf/proxy_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package proxyconf_test
    21  
    22  import (
    23  	"net/http"
    24  	"net/url"
    25  	"testing"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/overlord/configstate/config"
    30  	"github.com/snapcore/snapd/overlord/configstate/proxyconf"
    31  	"github.com/snapcore/snapd/overlord/state"
    32  )
    33  
    34  func TestT(t *testing.T) { TestingT(t) }
    35  
    36  type proxyconfSuite struct{}
    37  
    38  var _ = Suite(&proxyconfSuite{})
    39  
    40  func (s *proxyconfSuite) TestProxySettingsNoSetting(c *C) {
    41  	st := state.New(nil)
    42  
    43  	req, err := http.NewRequest("GET", "http://example.com", nil)
    44  	c.Assert(err, IsNil)
    45  
    46  	expected, err := http.ProxyFromEnvironment(req)
    47  	c.Assert(err, IsNil)
    48  
    49  	proxyConf := proxyconf.New(st)
    50  	proxy, err := proxyConf.Conf(req)
    51  	c.Assert(err, IsNil)
    52  	c.Check(proxy, DeepEquals, expected)
    53  }
    54  
    55  func (s *proxyconfSuite) TestProxySettings(c *C) {
    56  	st := state.New(nil)
    57  
    58  	req, err := http.NewRequest("GET", "http://example.com", nil)
    59  	c.Assert(err, IsNil)
    60  
    61  	st.Lock()
    62  	tr := config.NewTransaction(st)
    63  	tr.Set("core", "proxy.http", "http://some-proxy:3128")
    64  	tr.Commit()
    65  	st.Unlock()
    66  
    67  	proxyConf := proxyconf.New(st)
    68  	proxy, err := proxyConf.Conf(req)
    69  	c.Assert(err, IsNil)
    70  	c.Check(proxy, DeepEquals, &url.URL{
    71  		Scheme: "http",
    72  		Host:   "some-proxy:3128",
    73  	})
    74  }