github.com/loafoe/cli@v7.1.0+incompatible/integration/helpers/fake_data.go (about)

     1  package helpers
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	. "github.com/onsi/gomega"
    11  	"github.com/onsi/gomega/ghttp"
    12  )
    13  
    14  // AddFiftyOneOrgs adds a mock handler to the given server which returns
    15  // 51 orgs on GET requests to /v3/organizations?order_by=name. It also
    16  // paginates, so page 2 can be requested with /v3/organizations?page=2&per_page=50.
    17  func AddFiftyOneOrgs(server *ghttp.Server) {
    18  	AddHandler(server,
    19  		http.MethodGet,
    20  		"/v3/organizations?order_by=name",
    21  		http.StatusOK,
    22  		[]byte(fmt.Sprintf(string(fixtureData("fifty-orgs-page-1.json")), server.URL())),
    23  	)
    24  
    25  	AddHandler(server,
    26  		http.MethodGet,
    27  		"/v3/organizations?page=2&per_page=50",
    28  		http.StatusOK,
    29  		fixtureData("fifty-orgs-page-2.json"),
    30  	)
    31  }
    32  
    33  // AddFiftyOneSpaces adds mock handlers to the given http server which includes
    34  // an organization which will contain 51 spaces
    35  func AddFiftyOneSpaces(server *ghttp.Server) {
    36  	AddHandler(server,
    37  		http.MethodGet,
    38  		"/v3/organizations?order_by=name",
    39  		http.StatusOK,
    40  		[]byte(fmt.Sprintf(string(fixtureData("fifty-spaces-org.json")), server.URL())),
    41  	)
    42  
    43  	AddHandler(server,
    44  		http.MethodGet,
    45  		"/v3/spaces?organization_guids=4305313e-d34e-4015-9a57-5550235cd6b0",
    46  		http.StatusOK,
    47  		[]byte(fmt.Sprintf(string(fixtureData("fifty-spaces-page-1.json")), server.URL())),
    48  	)
    49  
    50  	AddHandler(server,
    51  		http.MethodGet,
    52  		"/v3/spaces?order_by=name&organization_guids=4305313e-d34e-4015-9a57-5550235cd6b0",
    53  		http.StatusOK,
    54  		[]byte(fmt.Sprintf(string(fixtureData("fifty-spaces-page-1.json")), server.URL())),
    55  	)
    56  
    57  	AddHandler(server,
    58  		http.MethodGet,
    59  		"/v3/spaces?organization_guids=4305313e-d34e-4015-9a57-5550235cd6b0&page=2&per_page=50",
    60  		http.StatusOK,
    61  		[]byte(fmt.Sprintf(string(fixtureData("fifty-spaces-page-2.json")), server.URL())),
    62  	)
    63  
    64  	AddHandler(server,
    65  		http.MethodGet,
    66  		"/v3/spaces?order_by=name&organization_guids=4305313e-d34e-4015-9a57-5550235cd6b0&page=2&per_page=50",
    67  		http.StatusOK,
    68  		[]byte(fmt.Sprintf(string(fixtureData("fifty-spaces-page-2.json")), server.URL())),
    69  	)
    70  }
    71  
    72  // AddEmptyPaginatedResponse adds a mock handler to the given server which returns
    73  // a response with no resources.
    74  func AddEmptyPaginatedResponse(server *ghttp.Server, path string) {
    75  	AddHandler(server,
    76  		http.MethodGet,
    77  		path,
    78  		http.StatusOK,
    79  		fixtureData("empty-paginated-response.json"),
    80  	)
    81  }
    82  
    83  func fixtureData(name string) []byte {
    84  	wd := os.Getenv("GOPATH")
    85  	fp := filepath.Join(wd, "src", "code.cloudfoundry.org", "cli", "integration", "helpers", "fixtures", name)
    86  	b, err := ioutil.ReadFile(fp)
    87  	Expect(err).ToNot(HaveOccurred())
    88  	return b
    89  }