|
| 1 | +package namecheap |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestDomainNameserversGetInfo(t *testing.T) { |
| 14 | + fakeResponse := ` |
| 15 | + <?xml version="1.0" encoding="UTF-8"?> |
| 16 | + <ApiResponse xmlns="http://api.namecheap.com/xml.response" Status="OK"> |
| 17 | + <Errors /> |
| 18 | + <RequestedCommand>namecheap.domains.ns.getInfo</RequestedCommand> |
| 19 | + <CommandResponse Type="namecheap.domains.ns.getInfo"> |
| 20 | + <DomainNSInfoResult Domain="domain.com" Nameserver="ns1.domain.com" IP="12.23.23.23"> |
| 21 | + <NameserverStatuses> |
| 22 | + <Status>OK</Status> |
| 23 | + <Status>Linked</Status> |
| 24 | + </NameserverStatuses> |
| 25 | + </DomainNSInfoResult> |
| 26 | + </CommandResponse> |
| 27 | + <Server>SERVER-NAME</Server> |
| 28 | + <GMTTimeDifference>+5</GMTTimeDifference> |
| 29 | + <ExecutionTime>32.76</ExecutionTime> |
| 30 | + </ApiResponse> |
| 31 | + ` |
| 32 | + |
| 33 | + t.Run("request_command", func(t *testing.T) { |
| 34 | + var sentBody url.Values |
| 35 | + |
| 36 | + mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 37 | + body, _ := ioutil.ReadAll(request.Body) |
| 38 | + query, _ := url.ParseQuery(string(body)) |
| 39 | + sentBody = query |
| 40 | + _, _ = writer.Write([]byte(fakeResponse)) |
| 41 | + })) |
| 42 | + defer mockServer.Close() |
| 43 | + |
| 44 | + client := setupClient(nil) |
| 45 | + client.BaseURL = mockServer.URL |
| 46 | + |
| 47 | + _, err := client.DomainsNS.GetInfo("specific-sld", "specific-tld", "ns1.domain.com") |
| 48 | + if err != nil { |
| 49 | + t.Fatal("Unable to get domain nameserver", err) |
| 50 | + } |
| 51 | + |
| 52 | + assert.Equal(t, "namecheap.domains.ns.getInfo", sentBody.Get("Command")) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("server_empty_response", func(t *testing.T) { |
| 56 | + fakeLocalResponse := "" |
| 57 | + |
| 58 | + mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 59 | + _, _ = writer.Write([]byte(fakeLocalResponse)) |
| 60 | + })) |
| 61 | + defer mockServer.Close() |
| 62 | + |
| 63 | + client := setupClient(nil) |
| 64 | + client.BaseURL = mockServer.URL |
| 65 | + |
| 66 | + _, err := client.DomainsNS.GetInfo("specific-sld", "specific-tld", "ns1.domain.com") |
| 67 | + |
| 68 | + assert.EqualError(t, err, "unable to parse server response: EOF") |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("server_non_xml_response", func(t *testing.T) { |
| 72 | + fakeLocalResponse := "non-xml response" |
| 73 | + |
| 74 | + mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 75 | + _, _ = writer.Write([]byte(fakeLocalResponse)) |
| 76 | + })) |
| 77 | + defer mockServer.Close() |
| 78 | + |
| 79 | + client := setupClient(nil) |
| 80 | + client.BaseURL = mockServer.URL |
| 81 | + |
| 82 | + _, err := client.DomainsNS.GetInfo("specific-sld", "specific-tld", "ns1.domain.com") |
| 83 | + |
| 84 | + assert.EqualError(t, err, "unable to parse server response: EOF") |
| 85 | + }) |
| 86 | + |
| 87 | + t.Run("server_broken_xml_response", func(t *testing.T) { |
| 88 | + fakeLocalResponse := "<broken></xml><response>" |
| 89 | + |
| 90 | + mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 91 | + _, _ = writer.Write([]byte(fakeLocalResponse)) |
| 92 | + })) |
| 93 | + defer mockServer.Close() |
| 94 | + |
| 95 | + client := setupClient(nil) |
| 96 | + client.BaseURL = mockServer.URL |
| 97 | + |
| 98 | + _, err := client.DomainsNS.GetInfo("specific-sld", "specific-tld", "ns1.domain.com") |
| 99 | + |
| 100 | + assert.EqualError(t, err, "unable to parse server response: expected element type <ApiResponse> but have <broken>") |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("server_respond_with_domain_not_found_error", func(t *testing.T) { |
| 104 | + fakeLocalResponse := ` |
| 105 | + <?xml version="1.0" encoding="utf-8"?> |
| 106 | + <ApiResponse Status="ERROR" xmlns="http://api.namecheap.com/xml.response"> |
| 107 | + <Errors> |
| 108 | + <Error Number="2019166">Domain not found</Error> |
| 109 | + </Errors> |
| 110 | + <Warnings /> |
| 111 | + <RequestedCommand>namecheap.domains.ns.getInfo</RequestedCommand> |
| 112 | + <Server>PHX01SBAPIEXT05</Server> |
| 113 | + <GMTTimeDifference>--4:00</GMTTimeDifference> |
| 114 | + <ExecutionTime>0.011</ExecutionTime> |
| 115 | + </ApiResponse> |
| 116 | + ` |
| 117 | + |
| 118 | + mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 119 | + _, _ = writer.Write([]byte(fakeLocalResponse)) |
| 120 | + })) |
| 121 | + defer mockServer.Close() |
| 122 | + |
| 123 | + client := setupClient(nil) |
| 124 | + client.BaseURL = mockServer.URL |
| 125 | + |
| 126 | + _, err := client.DomainsNS.GetInfo("specific-sld", "specific-tld", "ns1.domain.com") |
| 127 | + |
| 128 | + assert.EqualError(t, err, "Domain not found (2019166)") |
| 129 | + }) |
| 130 | + |
| 131 | + t.Run("server_respond_with_domain_not_associated_with_account_error", func(t *testing.T) { |
| 132 | + fakeLocalResponse := ` |
| 133 | + <?xml version="1.0" encoding="utf-8"?> |
| 134 | + <ApiResponse Status="ERROR" xmlns="http://api.namecheap.com/xml.response"> |
| 135 | + <Errors> |
| 136 | + <Error Number="2016166">Domain is not associated with your account</Error> |
| 137 | + </Errors> |
| 138 | + <Warnings /> |
| 139 | + <RequestedCommand>namecheap.domains.ns.getInfo</RequestedCommand> |
| 140 | + <Server>PHX01SBAPIEXT05</Server> |
| 141 | + <GMTTimeDifference>--4:00</GMTTimeDifference> |
| 142 | + <ExecutionTime>0.011</ExecutionTime> |
| 143 | + </ApiResponse> |
| 144 | + ` |
| 145 | + |
| 146 | + mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 147 | + _, _ = writer.Write([]byte(fakeLocalResponse)) |
| 148 | + })) |
| 149 | + defer mockServer.Close() |
| 150 | + |
| 151 | + client := setupClient(nil) |
| 152 | + client.BaseURL = mockServer.URL |
| 153 | + |
| 154 | + _, err := client.DomainsNS.GetInfo("specific-sld", "specific-tld", "ns1.domain.com") |
| 155 | + |
| 156 | + assert.EqualError(t, err, "Domain is not associated with your account (2016166)") |
| 157 | + }) |
| 158 | + |
| 159 | + t.Run("server_respond_with_error_from_enom", func(t *testing.T) { |
| 160 | + fakeLocalResponse := ` |
| 161 | + <?xml version="1.0" encoding="utf-8"?> |
| 162 | + <ApiResponse Status="ERROR" xmlns="http://api.namecheap.com/xml.response"> |
| 163 | + <Errors> |
| 164 | + <Error Number="3031510">Error From Enom when Errorcount <> 0</Error> |
| 165 | + </Errors> |
| 166 | + <Warnings /> |
| 167 | + <RequestedCommand>namecheap.domains.ns.getInfo</RequestedCommand> |
| 168 | + <Server>PHX01SBAPIEXT05</Server> |
| 169 | + <GMTTimeDifference>--4:00</GMTTimeDifference> |
| 170 | + <ExecutionTime>0.011</ExecutionTime> |
| 171 | + </ApiResponse> |
| 172 | + ` |
| 173 | + |
| 174 | + mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 175 | + _, _ = writer.Write([]byte(fakeLocalResponse)) |
| 176 | + })) |
| 177 | + defer mockServer.Close() |
| 178 | + |
| 179 | + client := setupClient(nil) |
| 180 | + client.BaseURL = mockServer.URL |
| 181 | + |
| 182 | + _, err := client.DomainsNS.GetInfo("specific-sld", "specific-tld", "ns1.domain.com") |
| 183 | + |
| 184 | + assert.EqualError(t, err, "Error From Enom when Errorcount <> 0 (3031510)") |
| 185 | + }) |
| 186 | + |
| 187 | + t.Run("server_respond_with_unknown_error_from_enom", func(t *testing.T) { |
| 188 | + fakeLocalResponse := ` |
| 189 | + <?xml version="1.0" encoding="utf-8"?> |
| 190 | + <ApiResponse Status="ERROR" xmlns="http://api.namecheap.com/xml.response"> |
| 191 | + <Errors> |
| 192 | + <Error Number="3050900">Unknown error from Enom</Error> |
| 193 | + </Errors> |
| 194 | + <Warnings /> |
| 195 | + <RequestedCommand>namecheap.domains.ns.getInfo</RequestedCommand> |
| 196 | + <Server>PHX01SBAPIEXT05</Server> |
| 197 | + <GMTTimeDifference>--4:00</GMTTimeDifference> |
| 198 | + <ExecutionTime>0.011</ExecutionTime> |
| 199 | + </ApiResponse> |
| 200 | + ` |
| 201 | + |
| 202 | + mockServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { |
| 203 | + _, _ = writer.Write([]byte(fakeLocalResponse)) |
| 204 | + })) |
| 205 | + defer mockServer.Close() |
| 206 | + |
| 207 | + client := setupClient(nil) |
| 208 | + client.BaseURL = mockServer.URL |
| 209 | + |
| 210 | + _, err := client.DomainsNS.GetInfo("specific-sld", "specific-tld", "ns1.domain.com") |
| 211 | + |
| 212 | + assert.EqualError(t, err, "Unknown error from Enom (3050900)") |
| 213 | + }) |
| 214 | +} |
0 commit comments