2020-02-25 20:56:40 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2020-02-25 20:56:40 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
func setupTest(t *testing.T) (*Client, *http.ServeMux) {
|
|
|
|
t.Helper()
|
2020-02-25 20:56:40 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
t.Cleanup(server.Close)
|
2020-02-25 20:56:40 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
client := NewClient(server.Client())
|
|
|
|
client.BaseURL = server.URL
|
|
|
|
|
|
|
|
return client, mux
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDomainService_GetAll(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
client, mux := setupTest(t)
|
2020-02-25 20:56:40 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
mux.HandleFunc("/v1/domains", func(rw http.ResponseWriter, req *http.Request) {
|
2020-02-25 20:56:40 +00:00
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-18 18:39:10 +00:00
|
|
|
file, err := os.Open("./fixtures/domains-GetAll.json")
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() { _ = file.Close() }()
|
|
|
|
|
|
|
|
_, err = io.Copy(rw, file)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
data, err := client.Domains.GetAll(context.Background(), nil)
|
2020-02-25 20:56:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []Domain{
|
2020-04-18 18:39:10 +00:00
|
|
|
{ID: 273301, Name: "aaa.wtf", TypeID: 1, Version: 9, Status: "ACTIVE"},
|
|
|
|
{ID: 273302, Name: "bbb.wtf", TypeID: 1, Version: 9, Status: "ACTIVE"},
|
|
|
|
{ID: 273303, Name: "ccc.wtf", TypeID: 1, Version: 9, Status: "ACTIVE"},
|
|
|
|
{ID: 273304, Name: "ddd.wtf", TypeID: 1, Version: 9, Status: "ACTIVE"},
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, data)
|
|
|
|
}
|
|
|
|
|
2020-04-18 18:39:10 +00:00
|
|
|
func TestDomainService_Search(t *testing.T) {
|
2021-11-01 23:52:38 +00:00
|
|
|
client, mux := setupTest(t)
|
2020-02-25 20:56:40 +00:00
|
|
|
|
2021-11-01 23:52:38 +00:00
|
|
|
mux.HandleFunc("/v1/domains/search", func(rw http.ResponseWriter, req *http.Request) {
|
2020-02-25 20:56:40 +00:00
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
http.Error(rw, "invalid method: "+req.Method, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-18 18:39:10 +00:00
|
|
|
file, err := os.Open("./fixtures/domains-Search.json")
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() { _ = file.Close() }()
|
|
|
|
|
|
|
|
_, err = io.Copy(rw, file)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
data, err := client.Domains.Search(context.Background(), Exact, "lego.wtf")
|
2020-02-25 20:56:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-18 18:39:10 +00:00
|
|
|
expected := []Domain{
|
|
|
|
{ID: 273302, Name: "lego.wtf", TypeID: 1, Version: 9, Status: "ACTIVE"},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, data)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|