github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/app_files/app_files_test.go (about)

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