github.com/naphatkrit/deis@v1.12.3/client/controller/models/domains/domains_test.go (about) 1 package domains 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "net/http/httptest" 8 "net/url" 9 "reflect" 10 "testing" 11 12 "github.com/deis/deis/client/controller/api" 13 "github.com/deis/deis/client/controller/client" 14 "github.com/deis/deis/version" 15 ) 16 17 const domainsFixture string = ` 18 { 19 "count": 1, 20 "next": null, 21 "previous": null, 22 "results": [ 23 { 24 "app": "example-go", 25 "created": "2014-01-01T00:00:00UTC", 26 "domain": "example.example.com", 27 "owner": "test", 28 "updated": "2014-01-01T00:00:00UTC" 29 } 30 ] 31 }` 32 33 const domainFixture string = ` 34 { 35 "app": "example-go", 36 "created": "2014-01-01T00:00:00UTC", 37 "domain": "example.example.com", 38 "owner": "test", 39 "updated": "2014-01-01T00:00:00UTC" 40 }` 41 42 const domainCreateExpected string = `{"domain":"example.example.com"}` 43 44 type fakeHTTPServer struct{} 45 46 func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) { 47 res.Header().Add("DEIS_API_VERSION", version.APIVersion) 48 49 if req.URL.Path == "/v1/apps/example-go/domains/" && req.Method == "GET" { 50 res.Write([]byte(domainsFixture)) 51 return 52 } 53 54 if req.URL.Path == "/v1/apps/example-go/domains/" && req.Method == "POST" { 55 body, err := ioutil.ReadAll(req.Body) 56 57 if err != nil { 58 fmt.Println(err) 59 res.WriteHeader(http.StatusInternalServerError) 60 res.Write(nil) 61 } 62 63 if string(body) != domainCreateExpected { 64 fmt.Printf("Expected '%s', Got '%s'\n", domainCreateExpected, body) 65 res.WriteHeader(http.StatusInternalServerError) 66 res.Write(nil) 67 return 68 } 69 70 res.WriteHeader(http.StatusCreated) 71 res.Write([]byte(domainFixture)) 72 return 73 } 74 75 if req.URL.Path == "/v1/apps/example-go/domains/test.com" && req.Method == "DELETE" { 76 res.WriteHeader(http.StatusNoContent) 77 res.Write([]byte(domainsFixture)) 78 return 79 } 80 81 fmt.Printf("Unrecognized URL %s\n", req.URL) 82 res.WriteHeader(http.StatusNotFound) 83 res.Write(nil) 84 } 85 86 func TestDomainsList(t *testing.T) { 87 t.Parallel() 88 89 expected := []api.Domain{ 90 api.Domain{ 91 App: "example-go", 92 Created: "2014-01-01T00:00:00UTC", 93 Domain: "example.example.com", 94 Owner: "test", 95 Updated: "2014-01-01T00:00:00UTC", 96 }, 97 } 98 99 handler := fakeHTTPServer{} 100 server := httptest.NewServer(handler) 101 defer server.Close() 102 103 u, err := url.Parse(server.URL) 104 105 if err != nil { 106 t.Fatal(err) 107 } 108 109 httpClient := client.CreateHTTPClient(false) 110 111 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 112 113 actual, _, err := List(&client, "example-go", 100) 114 115 if err != nil { 116 t.Fatal(err) 117 } 118 119 if !reflect.DeepEqual(expected, actual) { 120 t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual)) 121 } 122 } 123 124 func TestDomainsAdd(t *testing.T) { 125 t.Parallel() 126 127 expected := api.Domain{ 128 App: "example-go", 129 Created: "2014-01-01T00:00:00UTC", 130 Domain: "example.example.com", 131 Owner: "test", 132 Updated: "2014-01-01T00:00:00UTC", 133 } 134 135 handler := fakeHTTPServer{} 136 server := httptest.NewServer(handler) 137 defer server.Close() 138 139 u, err := url.Parse(server.URL) 140 141 if err != nil { 142 t.Fatal(err) 143 } 144 145 httpClient := client.CreateHTTPClient(false) 146 147 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 148 149 actual, err := New(&client, "example-go", "example.example.com") 150 151 if err != nil { 152 t.Fatal(err) 153 } 154 155 if !reflect.DeepEqual(expected, actual) { 156 t.Error(fmt.Errorf("Expected %v, Got %v", expected, actual)) 157 } 158 } 159 160 func TestDomainsRemove(t *testing.T) { 161 t.Parallel() 162 163 handler := fakeHTTPServer{} 164 server := httptest.NewServer(handler) 165 defer server.Close() 166 167 u, err := url.Parse(server.URL) 168 169 if err != nil { 170 t.Fatal(err) 171 } 172 173 httpClient := client.CreateHTTPClient(false) 174 175 client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"} 176 177 if err = Delete(&client, "example-go", "test.com"); err != nil { 178 t.Fatal(err) 179 } 180 }