github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/cf/api/appfiles/app_files_test.go (about)

     1  package appfiles_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"time"
     8  
     9  	"code.cloudfoundry.org/cli/cf/api/apifakes"
    10  	"code.cloudfoundry.org/cli/cf/net"
    11  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    12  	testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration"
    13  	testnet "code.cloudfoundry.org/cli/cf/util/testhelpers/net"
    14  
    15  	. "code.cloudfoundry.org/cli/cf/api/appfiles"
    16  	"code.cloudfoundry.org/cli/cf/trace/tracefakes"
    17  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    18  	. "github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("AppFilesRepository", func() {
    23  	It("lists files", func() {
    24  		expectedResponse := "file 1\n file 2\n file 3"
    25  
    26  		listFilesEndpoint := func(writer http.ResponseWriter, request *http.Request) {
    27  			methodMatches := request.Method == "GET"
    28  			pathMatches := request.URL.Path == "/some/path"
    29  
    30  			if !methodMatches || !pathMatches {
    31  				fmt.Printf("One of the matchers did not match. Method [%t] Path [%t]",
    32  					methodMatches, pathMatches)
    33  
    34  				writer.WriteHeader(http.StatusInternalServerError)
    35  				return
    36  			}
    37  
    38  			writer.WriteHeader(http.StatusOK)
    39  			fmt.Fprint(writer, expectedResponse)
    40  		}
    41  
    42  		listFilesServer := httptest.NewServer(http.HandlerFunc(listFilesEndpoint))
    43  		defer listFilesServer.Close()
    44  
    45  		req := apifakes.NewCloudControllerTestRequest(testnet.TestRequest{
    46  			Method: "GET",
    47  			Path:   "/v2/apps/my-app-guid/instances/1/files/some/path",
    48  			Response: testnet.TestResponse{
    49  				Status: http.StatusTemporaryRedirect,
    50  				Header: http.Header{
    51  					"Location": {fmt.Sprintf("%s/some/path", listFilesServer.URL)},
    52  				},
    53  			},
    54  		})
    55  
    56  		listFilesRedirectServer, handler := testnet.NewServer([]testnet.TestRequest{req})
    57  		defer listFilesRedirectServer.Close()
    58  
    59  		configRepo := testconfig.NewRepositoryWithDefaults()
    60  		configRepo.SetAPIEndpoint(listFilesRedirectServer.URL)
    61  
    62  		gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "")
    63  		repo := NewCloudControllerAppFilesRepository(configRepo, gateway)
    64  		list, err := repo.ListFiles("my-app-guid", 1, "some/path")
    65  
    66  		Expect(handler).To(HaveAllRequestsCalled())
    67  		Expect(err).ToNot(HaveOccurred())
    68  		Expect(list).To(Equal(expectedResponse))
    69  	})
    70  })