chore(tfstated): simplify some code
This commit is contained in:
parent
3319e74279
commit
5959766cbd
7 changed files with 55 additions and 56 deletions
|
@ -11,20 +11,20 @@ import (
|
||||||
func TestDelete(t *testing.T) {
|
func TestDelete(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
method string
|
method string
|
||||||
uri *url.URL
|
uri url.URL
|
||||||
body io.Reader
|
body io.Reader
|
||||||
expect string
|
expect string
|
||||||
status int
|
status int
|
||||||
msg string
|
msg string
|
||||||
}{
|
}{
|
||||||
{"DELETE", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
{"DELETE", url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
||||||
{"DELETE", &url.URL{Path: "/non_existent_delete"}, nil, "", http.StatusNotFound, "non existent"},
|
{"DELETE", url.URL{Path: "/non_existent_delete"}, nil, "", http.StatusNotFound, "non existent"},
|
||||||
{"POST", &url.URL{Path: "/test_delete"}, strings.NewReader("the_test_delete"), "", http.StatusOK, "/test_delete"},
|
{"POST", url.URL{Path: "/test_delete"}, strings.NewReader("the_test_delete"), "", http.StatusOK, "/test_delete"},
|
||||||
{"DELETE", &url.URL{Path: "/test_delete"}, nil, "", http.StatusOK, "/test_delete"},
|
{"DELETE", url.URL{Path: "/test_delete"}, nil, "", http.StatusOK, "/test_delete"},
|
||||||
{"DELETE", &url.URL{Path: "/test_delete"}, nil, "", http.StatusNotFound, "/test_delete"},
|
{"DELETE", url.URL{Path: "/test_delete"}, nil, "", http.StatusNotFound, "/test_delete"},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) {
|
runHTTPRequest(tt.method, &tt.uri, tt.body, func(r *http.Response, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
||||||
} else if r.StatusCode != tt.status {
|
} else if r.StatusCode != tt.status {
|
||||||
|
@ -32,7 +32,7 @@ func TestDelete(t *testing.T) {
|
||||||
} else if tt.expect != "" {
|
} else if tt.expect != "" {
|
||||||
if body, err := io.ReadAll(r.Body); err != nil {
|
if body, err := io.ReadAll(r.Body); err != nil {
|
||||||
t.Fatalf("failed to read body with error: %+v", err)
|
t.Fatalf("failed to read body with error: %+v", err)
|
||||||
} else if strings.Compare(string(body), tt.expect) != 0 {
|
} else if string(body) != tt.expect {
|
||||||
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,19 +11,19 @@ import (
|
||||||
func TestGet(t *testing.T) {
|
func TestGet(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
method string
|
method string
|
||||||
uri *url.URL
|
uri url.URL
|
||||||
body io.Reader
|
body io.Reader
|
||||||
expect string
|
expect string
|
||||||
status int
|
status int
|
||||||
msg string
|
msg string
|
||||||
}{
|
}{
|
||||||
{"GET", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
{"GET", url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
||||||
{"GET", &url.URL{Path: "/non_existent_get"}, strings.NewReader(""), "", http.StatusOK, "non existent"},
|
{"GET", url.URL{Path: "/non_existent_get"}, strings.NewReader(""), "", http.StatusOK, "non existent"},
|
||||||
{"POST", &url.URL{Path: "/test_get"}, strings.NewReader("the_test_get"), "", http.StatusOK, "/test_get"},
|
{"POST", url.URL{Path: "/test_get"}, strings.NewReader("the_test_get"), "", http.StatusOK, "/test_get"},
|
||||||
{"GET", &url.URL{Path: "/test_get"}, nil, "the_test_get", http.StatusOK, "/test_get"},
|
{"GET", url.URL{Path: "/test_get"}, nil, "the_test_get", http.StatusOK, "/test_get"},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) {
|
runHTTPRequest(tt.method, &tt.uri, tt.body, func(r *http.Response, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
||||||
} else if r.StatusCode != tt.status {
|
} else if r.StatusCode != tt.status {
|
||||||
|
@ -31,7 +31,7 @@ func TestGet(t *testing.T) {
|
||||||
} else if tt.expect != "" {
|
} else if tt.expect != "" {
|
||||||
if body, err := io.ReadAll(r.Body); err != nil {
|
if body, err := io.ReadAll(r.Body); err != nil {
|
||||||
t.Fatalf("failed to read body with error: %+v", err)
|
t.Fatalf("failed to read body with error: %+v", err)
|
||||||
} else if strings.Compare(string(body), tt.expect) != 0 {
|
} else if string(body) != tt.expect {
|
||||||
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,24 +11,24 @@ import (
|
||||||
func TestLock(t *testing.T) {
|
func TestLock(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
method string
|
method string
|
||||||
uri *url.URL
|
uri url.URL
|
||||||
body io.Reader
|
body io.Reader
|
||||||
expect string
|
expect string
|
||||||
status int
|
status int
|
||||||
msg string
|
msg string
|
||||||
}{
|
}{
|
||||||
{"LOCK", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
{"LOCK", url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
||||||
{"LOCK", &url.URL{Path: "/non_existent_lock"}, nil, "", http.StatusBadRequest, "no lock data on non existent state"},
|
{"LOCK", url.URL{Path: "/non_existent_lock"}, nil, "", http.StatusBadRequest, "no lock data on non existent state"},
|
||||||
{"LOCK", &url.URL{Path: "/non_existent_lock"}, strings.NewReader("{}"), "", http.StatusBadRequest, "invalid lock data on non existent state"},
|
{"LOCK", url.URL{Path: "/non_existent_lock"}, strings.NewReader("{}"), "", http.StatusBadRequest, "invalid lock data on non existent state"},
|
||||||
{"LOCK", &url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid lock data on non existent state should create it empty"},
|
{"LOCK", url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid lock data on non existent state should create it empty"},
|
||||||
{"GET", &url.URL{Path: "/test_lock"}, nil, "", http.StatusOK, "/test_lock"},
|
{"GET", url.URL{Path: "/test_lock"}, nil, "", http.StatusOK, "/test_lock"},
|
||||||
{"LOCK", &url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"\"}"), "", http.StatusBadRequest, "invalid lock data on already locked state"},
|
{"LOCK", url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"\"}"), "", http.StatusBadRequest, "invalid lock data on already locked state"},
|
||||||
{"LOCK", &url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid lock data on already locked state"},
|
{"LOCK", url.URL{Path: "/test_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid lock data on already locked state"},
|
||||||
{"POST", &url.URL{Path: "/test_lock", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_lock"), "", http.StatusOK, "/test_lock"},
|
{"POST", url.URL{Path: "/test_lock", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_lock"), "", http.StatusOK, "/test_lock"},
|
||||||
{"GET", &url.URL{Path: "/test_lock"}, nil, "the_test_lock", http.StatusOK, "/test_lock"},
|
{"GET", url.URL{Path: "/test_lock"}, nil, "the_test_lock", http.StatusOK, "/test_lock"},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) {
|
runHTTPRequest(tt.method, &tt.uri, tt.body, func(r *http.Response, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
||||||
} else if r.StatusCode != tt.status {
|
} else if r.StatusCode != tt.status {
|
||||||
|
@ -36,7 +36,7 @@ func TestLock(t *testing.T) {
|
||||||
} else if tt.expect != "" {
|
} else if tt.expect != "" {
|
||||||
if body, err := io.ReadAll(r.Body); err != nil {
|
if body, err := io.ReadAll(r.Body); err != nil {
|
||||||
t.Fatalf("failed to read body with error: %+v", err)
|
t.Fatalf("failed to read body with error: %+v", err)
|
||||||
} else if strings.Compare(string(body), tt.expect) != 0 {
|
} else if string(body) != tt.expect {
|
||||||
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
var baseURI = &url.URL{
|
var baseURI = url.URL{
|
||||||
Host: "127.0.0.1:8081",
|
Host: "127.0.0.1:8081",
|
||||||
Path: "/",
|
Path: "/",
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
|
|
|
@ -11,28 +11,28 @@ import (
|
||||||
func TestPost(t *testing.T) {
|
func TestPost(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
method string
|
method string
|
||||||
uri *url.URL
|
uri url.URL
|
||||||
body io.Reader
|
body io.Reader
|
||||||
expect string
|
expect string
|
||||||
status int
|
status int
|
||||||
msg string
|
msg string
|
||||||
}{
|
}{
|
||||||
{"POST", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
{"POST", url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
||||||
{"POST", &url.URL{Path: "/test_post"}, nil, "", http.StatusBadRequest, "without a body"},
|
{"POST", url.URL{Path: "/test_post"}, nil, "", http.StatusBadRequest, "without a body"},
|
||||||
{"POST", &url.URL{Path: "/test_post"}, strings.NewReader("the_test_post"), "", http.StatusOK, "without lock ID in query string"},
|
{"POST", url.URL{Path: "/test_post"}, strings.NewReader("the_test_post"), "", http.StatusOK, "without lock ID in query string"},
|
||||||
{"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"},
|
{"GET", url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"},
|
||||||
{"POST", &url.URL{Path: "/test_post", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_post2"), "", http.StatusConflict, "with a lock ID on an unlocked state"},
|
{"POST", url.URL{Path: "/test_post", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_post2"), "", http.StatusConflict, "with a lock ID on an unlocked state"},
|
||||||
{"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"},
|
{"GET", url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"},
|
||||||
{"LOCK", &url.URL{Path: "/test_post"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "/test_post"},
|
{"LOCK", url.URL{Path: "/test_post"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "/test_post"},
|
||||||
{"POST", &url.URL{Path: "/test_post", RawQuery: "ID=ffffffff-ffff-ffff-ffff-ffffffffffff"}, strings.NewReader("the_test_post3"), "", http.StatusConflict, "with a wrong lock ID on a locked state"},
|
{"POST", url.URL{Path: "/test_post", RawQuery: "ID=ffffffff-ffff-ffff-ffff-ffffffffffff"}, strings.NewReader("the_test_post3"), "", http.StatusConflict, "with a wrong lock ID on a locked state"},
|
||||||
{"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"},
|
{"GET", url.URL{Path: "/test_post"}, nil, "the_test_post", http.StatusOK, "/test_post"},
|
||||||
{"POST", &url.URL{Path: "/test_post", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_post4"), "", http.StatusOK, "with a correct lock ID on a locked state"},
|
{"POST", url.URL{Path: "/test_post", RawQuery: "ID=00000000-0000-0000-0000-000000000000"}, strings.NewReader("the_test_post4"), "", http.StatusOK, "with a correct lock ID on a locked state"},
|
||||||
{"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post4", http.StatusOK, "/test_post"},
|
{"GET", url.URL{Path: "/test_post"}, nil, "the_test_post4", http.StatusOK, "/test_post"},
|
||||||
{"POST", &url.URL{Path: "/test_post"}, strings.NewReader("the_test_post5"), "", http.StatusOK, "without lock ID in query string on a locked state"},
|
{"POST", url.URL{Path: "/test_post"}, strings.NewReader("the_test_post5"), "", http.StatusOK, "without lock ID in query string on a locked state"},
|
||||||
{"GET", &url.URL{Path: "/test_post"}, nil, "the_test_post5", http.StatusOK, "/test_post"},
|
{"GET", url.URL{Path: "/test_post"}, nil, "the_test_post5", http.StatusOK, "/test_post"},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) {
|
runHTTPRequest(tt.method, &tt.uri, tt.body, func(r *http.Response, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
||||||
} else if r.StatusCode != tt.status {
|
} else if r.StatusCode != tt.status {
|
||||||
|
@ -40,7 +40,7 @@ func TestPost(t *testing.T) {
|
||||||
} else if tt.expect != "" {
|
} else if tt.expect != "" {
|
||||||
if body, err := io.ReadAll(r.Body); err != nil {
|
if body, err := io.ReadAll(r.Body); err != nil {
|
||||||
t.Fatalf("failed to read body with error: %+v", err)
|
t.Fatalf("failed to read body with error: %+v", err)
|
||||||
} else if strings.Compare(string(body), tt.expect) != 0 {
|
} else if string(body) != tt.expect {
|
||||||
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,22 +11,22 @@ import (
|
||||||
func TestUnlock(t *testing.T) {
|
func TestUnlock(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
method string
|
method string
|
||||||
uri *url.URL
|
uri url.URL
|
||||||
body io.Reader
|
body io.Reader
|
||||||
expect string
|
expect string
|
||||||
status int
|
status int
|
||||||
msg string
|
msg string
|
||||||
}{
|
}{
|
||||||
{"UNLOCK", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
{"UNLOCK", url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
||||||
{"UNLOCK", &url.URL{Path: "/non_existent_lock"}, nil, "", http.StatusBadRequest, "no lock data on non existent state"},
|
{"UNLOCK", url.URL{Path: "/non_existent_lock"}, nil, "", http.StatusBadRequest, "no lock data on non existent state"},
|
||||||
{"UNLOCK", &url.URL{Path: "/non_existent_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid lock data on non existent state"},
|
{"UNLOCK", url.URL{Path: "/non_existent_lock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid lock data on non existent state"},
|
||||||
{"LOCK", &url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid lock data on non existent state should create it empty"},
|
{"LOCK", url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid lock data on non existent state should create it empty"},
|
||||||
{"UNLOCK", &url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF\"}"), "", http.StatusConflict, "valid but wrong lock data on a locked state"},
|
{"UNLOCK", url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF\"}"), "", http.StatusConflict, "valid but wrong lock data on a locked state"},
|
||||||
{"UNLOCK", &url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid and correct lock data on a locked state"},
|
{"UNLOCK", url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusOK, "valid and correct lock data on a locked state"},
|
||||||
{"UNLOCK", &url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid and correct lock data on a now unlocked state"},
|
{"UNLOCK", url.URL{Path: "/test_unlock"}, strings.NewReader("{\"ID\":\"00000000-0000-0000-0000-000000000000\"}"), "", http.StatusConflict, "valid and correct lock data on a now unlocked state"},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) {
|
runHTTPRequest(tt.method, &tt.uri, tt.body, func(r *http.Response, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
||||||
} else if r.StatusCode != tt.status {
|
} else if r.StatusCode != tt.status {
|
||||||
|
@ -34,7 +34,7 @@ func TestUnlock(t *testing.T) {
|
||||||
} else if tt.expect != "" {
|
} else if tt.expect != "" {
|
||||||
if body, err := io.ReadAll(r.Body); err != nil {
|
if body, err := io.ReadAll(r.Body); err != nil {
|
||||||
t.Fatalf("failed to read body with error: %+v", err)
|
t.Fatalf("failed to read body with error: %+v", err)
|
||||||
} else if strings.Compare(string(body), tt.expect) != 0 {
|
} else if string(body) != tt.expect {
|
||||||
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package scrypto
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,7 +17,7 @@ func TestAES256KeyHex(t *testing.T) {
|
||||||
if slices.Compare(testKey, key[:]) != 0 {
|
if slices.Compare(testKey, key[:]) != 0 {
|
||||||
t.Errorf("got %v, wanted %v", testKey, key[:])
|
t.Errorf("got %v, wanted %v", testKey, key[:])
|
||||||
}
|
}
|
||||||
if strings.Compare(testKeyHex, key.Hex()) != 0 {
|
if testKeyHex != key.Hex() {
|
||||||
t.Errorf("got %v, wanted %v", testKeyHex, key.Hex())
|
t.Errorf("got %v, wanted %v", testKeyHex, key.Hex())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue