github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/worker/gclient/connection/connection_hijacker_test.go (about)

     1  package connection_test
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"errors"
     7  	"net"
     8  	"net/url"
     9  
    10  	"code.cloudfoundry.org/garden/routes"
    11  	"github.com/pf-qiu/concourse/v6/atc/worker/gclient/connection"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	"github.com/tedsuo/rata"
    15  )
    16  
    17  var _ = Describe("ConnectionHijacker", func() {
    18  	Describe("constructing hijacker with a dialer", func() {
    19  		var hijackStreamer connection.HijackStreamer
    20  
    21  		BeforeEach(func() {
    22  			dialer := func(string, string) (net.Conn, error) {
    23  				return nil, errors.New("oh no I am hijacked")
    24  			}
    25  			hijackStreamer = connection.NewHijackStreamerWithDialer(dialer)
    26  		})
    27  
    28  		Context("when Hijack is called", func() {
    29  			It("should use the dialer", func() {
    30  				_, _, err := hijackStreamer.Hijack(
    31  					context.TODO(),
    32  					routes.Run,
    33  					new(bytes.Buffer),
    34  					rata.Params{
    35  						"handle": "some-test-handle",
    36  					},
    37  					nil,
    38  					"application/json",
    39  				)
    40  				Expect(err).To(MatchError("oh no I am hijacked"))
    41  			})
    42  		})
    43  
    44  		Context("when Stream is called", func() {
    45  			It("should use the dialer", func() {
    46  				_, err := hijackStreamer.Stream(
    47  					routes.Run,
    48  					new(bytes.Buffer),
    49  					rata.Params{
    50  						"handle": "some-test-handle",
    51  					},
    52  					nil,
    53  					"application/json",
    54  				)
    55  
    56  				pathError, ok := err.(*url.Error)
    57  				Expect(ok).To(BeTrue())
    58  				Expect(pathError.Err).To(MatchError("oh no I am hijacked"))
    59  			})
    60  		})
    61  	})
    62  
    63  })