Skip to content

Commit

Permalink
Add unit test for unpackUpdateRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
lilic committed Apr 2, 2018
1 parent 274e4aa commit 4f24ec1
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/rest/apisurface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package rest

import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)

func TestUnpackUpdateRequest(t *testing.T) {
instanceID := "i1234"
serviceID := "s1234"
planID := "p1234"
acceptsIncomplete := true

fakeUpdateReq := createFakeUpdateRequest(serviceID, planID, acceptsIncomplete)
unpackReq, err := unpackUpdateRequest(fakeUpdateReq, map[string]string{"instance_id": instanceID})
if err != nil {
t.Fatalf("Unpacking update request: %v", err)
}

if unpackReq.InstanceID != instanceID {
t.Fatalf("InstanceID was unpacked unsuccessfully. Expecting %s got %s", instanceID, unpackReq.InstanceID)
}

if unpackReq.ServiceID != serviceID {
t.Fatalf("PlanID was unpacked unsuccessfully. Expecting %s got %s", serviceID, unpackReq.ServiceID)
}

if *unpackReq.PlanID != planID {
t.Fatalf("PlanID was unpacked unsuccessfully. Expecting %s got %s", planID, *unpackReq.PlanID)
}

if unpackReq.AcceptsIncomplete != acceptsIncomplete {
t.Fatalf("AcceptsIncomplete was unpacked unsuccessfully. Expecting %t got %t", acceptsIncomplete, unpackReq.AcceptsIncomplete)
}
}

func createFakeUpdateRequest(s, p string, a bool) *http.Request {
data := fmt.Sprintf(`{
"context": {
"platform": "kubernetes",
"some_field": "some-contextual-data"
},
"service_id": "%s",
"plan_id": "%s",
"parameters": {
"parameter1": 1,
"parameter2": "foo"
},
"previous_values": {
"plan_id": "old-plan-id-here",
"service_id": "service-id-here",
"organization_id": "org-guid-here",
"space_id": "space-guid-here"
}
}`, s, p)

r := bytes.NewBufferString(data)
uri := fmt.Sprintf("/v2/service_instances/i1234?accepts_incomplete=%t", a)

return httptest.NewRequest("PATCH", uri, r)
}

0 comments on commit 4f24ec1

Please sign in to comment.