github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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?organization_guids=4305313e-d34e-4015-9a57-5550235cd6b0&page=2&per_page=50",
    53  		http.StatusOK,
    54  		[]byte(fmt.Sprintf(string(fixtureData("fifty-spaces-page-2.json")), server.URL())),
    55  	)
    56  }
    57  
    58  // AddEmptyPaginatedResponse adds a mock handler to the given server which returns
    59  // a response with no resources.
    60  func AddEmptyPaginatedResponse(server *ghttp.Server, path string) {
    61  	AddHandler(server,
    62  		http.MethodGet,
    63  		path,
    64  		http.StatusOK,
    65  		fixtureData("empty-paginated-response.json"),
    66  	)
    67  }
    68  
    69  func fixtureData(name string) []byte {
    70  	wd := os.Getenv("GOPATH")
    71  	fp := filepath.Join(wd, "src", "code.cloudfoundry.org", "cli", "integration", "helpers", "fixtures", name)
    72  	b, err := ioutil.ReadFile(fp)
    73  	Expect(err).ToNot(HaveOccurred())
    74  	return b
    75  }