github.com/hairyhenderson/templater@v3.5.0+incompatible/tests/integration/datasources_http_test.go (about)

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"net"
     7  	"net/http"
     8  
     9  	. "gopkg.in/check.v1"
    10  
    11  	"github.com/gotestyourself/gotestyourself/icmd"
    12  )
    13  
    14  type DatasourcesHTTPSuite struct {
    15  	l *net.TCPListener
    16  }
    17  
    18  var _ = Suite(&DatasourcesHTTPSuite{})
    19  
    20  func (s *DatasourcesHTTPSuite) SetUpSuite(c *C) {
    21  	var err error
    22  	s.l, err = net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1")})
    23  	handle(c, err)
    24  
    25  	http.HandleFunc("/", mirrorHandler)
    26  	http.HandleFunc("/not.json", typeHandler("application/yaml", "value: notjson\n"))
    27  	http.HandleFunc("/foo", typeHandler("application/json", `{"value": "json"}`))
    28  	http.HandleFunc("/actually.json", typeHandler("", `{"value": "json"}`))
    29  	http.HandleFunc("/bogus.csv", typeHandler("text/plain", `{"value": "json"}`))
    30  	go http.Serve(s.l, nil)
    31  }
    32  
    33  func (s *DatasourcesHTTPSuite) TearDownSuite(c *C) {
    34  	s.l.Close()
    35  }
    36  
    37  func (s *DatasourcesHTTPSuite) TestReportsVersion(c *C) {
    38  	result := icmd.RunCommand(GomplateBin,
    39  		"-d", "foo=http://"+s.l.Addr().String()+"/",
    40  		"-H", "foo=Foo:bar",
    41  		"-i", "{{ index (ds `foo`).headers.Foo 0 }}")
    42  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"})
    43  
    44  	result = icmd.RunCommand(GomplateBin,
    45  		"-H", "foo=Foo:bar",
    46  		"-i", "{{defineDatasource `foo` `http://"+s.l.Addr().String()+"/`}}{{ index (ds `foo`).headers.Foo 0 }}")
    47  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "bar"})
    48  
    49  	result = icmd.RunCommand(GomplateBin,
    50  		"-i", "{{ $d := ds `http://"+s.l.Addr().String()+"/`}}{{ index (index $d.headers `Accept-Encoding`) 0 }}")
    51  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "gzip"})
    52  }
    53  
    54  func (s *DatasourcesHTTPSuite) TestTypeOverridePrecedence(c *C) {
    55  	result := icmd.RunCommand(GomplateBin,
    56  		"-d", "foo=http://"+s.l.Addr().String()+"/foo",
    57  		"-i", "{{ (ds `foo`).value }}")
    58  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"})
    59  
    60  	result = icmd.RunCommand(GomplateBin,
    61  		"-d", "foo=http://"+s.l.Addr().String()+"/not.json",
    62  		"-i", "{{ (ds `foo`).value }}")
    63  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "notjson"})
    64  
    65  	result = icmd.RunCommand(GomplateBin,
    66  		"-d", "foo=http://"+s.l.Addr().String()+"/actually.json",
    67  		"-i", "{{ (ds `foo`).value }}")
    68  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"})
    69  
    70  	result = icmd.RunCommand(GomplateBin,
    71  		"-d", "foo=http://"+s.l.Addr().String()+"/bogus.csv?type=application/json",
    72  		"-i", "{{ (ds `foo`).value }}")
    73  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"})
    74  }
    75  
    76  func (s *DatasourcesHTTPSuite) TestAppendQueryAfterSubPaths(c *C) {
    77  	result := icmd.RunCommand(GomplateBin,
    78  		"-d", "foo=http://"+s.l.Addr().String()+"/?type=application/json",
    79  		"-i", "{{ (ds `foo` `bogus.csv`).value }}")
    80  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "json"})
    81  }