Feat: add more tests
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package tests
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_02_GetImages(t *testing.T) {
|
||||
resp, err := client.R().
|
||||
Get("http://localhost:8080/api/aliases")
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to fetch aliases")
|
||||
}
|
||||
}
|
||||
37
tests/02_postImage_test.go
Normal file
37
tests/02_postImage_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type postImagePayload struct {
|
||||
Id int64 `json:"id"`
|
||||
Extension string `json:"extension"`
|
||||
}
|
||||
|
||||
var rawImage []byte
|
||||
var image postImagePayload
|
||||
|
||||
func Test_02_PostImage(t *testing.T) {
|
||||
f, err := os.Open("resources/huh.png")
|
||||
if err != nil {
|
||||
t.Fatal("failed to open test png")
|
||||
}
|
||||
rawImage, err = io.ReadAll(f)
|
||||
if err != nil {
|
||||
t.Fatal("failed to read from file")
|
||||
}
|
||||
|
||||
resp, err := client.R().
|
||||
SetBody(rawImage).
|
||||
SetResult(&image).
|
||||
Post("http://localhost:8080/api/image")
|
||||
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to post image")
|
||||
}
|
||||
}
|
||||
25
tests/03_putImageAliases_test.go
Normal file
25
tests/03_putImageAliases_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type putImageAliasPayload struct {
|
||||
Aliases []string `json:"aliases"`
|
||||
}
|
||||
|
||||
func Test_03_PutImageAliases(t *testing.T) {
|
||||
payload := putImageAliasPayload{
|
||||
Aliases: []string{"huh"},
|
||||
}
|
||||
resp, err := client.R().
|
||||
SetBody(payload).
|
||||
Put(fmt.Sprintf("http://localhost:8080/api/image/%d/aliases",
|
||||
image.Id))
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to put image alias")
|
||||
}
|
||||
}
|
||||
26
tests/04_getAliases_test.go
Normal file
26
tests/04_getAliases_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type aliasPayload struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
var aliases []aliasPayload
|
||||
|
||||
func Test_04_GetAliases(t *testing.T) {
|
||||
resp, err := client.R().
|
||||
SetResult(&aliases).
|
||||
Get("http://localhost:8080/api/aliases")
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to get aliases")
|
||||
}
|
||||
if len(aliases) == 0 {
|
||||
t.Fatal("no aliases")
|
||||
}
|
||||
}
|
||||
28
tests/05_getImages_test.go
Normal file
28
tests/05_getImages_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_05_GetImages(t *testing.T) {
|
||||
t.Run("check with alias id", func(t *testing.T) {
|
||||
resp, err := client.R().
|
||||
SetQueryParam("aliases", strconv.FormatInt(aliases[0].Id, 10)).
|
||||
Get("http://localhost:8080/api/images")
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to get images by alias id")
|
||||
}
|
||||
})
|
||||
t.Run("check with image id", func(t *testing.T) {
|
||||
resp, err := client.R().
|
||||
SetQueryParam("images", strconv.FormatInt(image.Id, 10)).
|
||||
Get("http://localhost:8080/api/images")
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to get images by image id")
|
||||
}
|
||||
})
|
||||
}
|
||||
21
tests/06_getImage_test.go
Normal file
21
tests/06_getImage_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_06_GetImage(t *testing.T) {
|
||||
resp, err := client.R().
|
||||
Get(fmt.Sprintf("http://localhost:8080/img/%d.%s",
|
||||
image.Id, image.Extension))
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Fatal("failed to get image")
|
||||
}
|
||||
|
||||
if bytes.Compare(rawImage, resp.Body()) != 0 {
|
||||
t.Fatal("image differ")
|
||||
}
|
||||
}
|
||||
29
tests/07_deleteAlias_test.go
Normal file
29
tests/07_deleteAlias_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_07_DeleteAlias(t *testing.T) {
|
||||
resp, err := client.R().
|
||||
Delete(fmt.Sprintf("http://localhost:8080/api/alias/%d",
|
||||
aliases[0].Id))
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to delete alias")
|
||||
}
|
||||
|
||||
resp, err = client.R().
|
||||
SetResult(&aliases).
|
||||
Get("http://localhost:8080/api/aliases")
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to get aliases")
|
||||
}
|
||||
|
||||
if len(aliases) > 0 {
|
||||
t.Fatal("alias not deleted")
|
||||
}
|
||||
}
|
||||
46
tests/08_deleteImage_test.go
Normal file
46
tests/08_deleteImage_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type getImagesPayload struct {
|
||||
Id int64 `json:"id"`
|
||||
}
|
||||
|
||||
func Test_08_DeleteImage(t *testing.T) {
|
||||
resp, err := client.R().
|
||||
SetBody(rawImage).
|
||||
SetResult(&image).
|
||||
Post("http://localhost:8080/api/image")
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to post image")
|
||||
}
|
||||
|
||||
resp, err = client.R().
|
||||
Delete(fmt.Sprintf("http://localhost:8080/api/image/%d",
|
||||
image.Id))
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to delete image")
|
||||
}
|
||||
|
||||
var imgs []getImagesPayload
|
||||
|
||||
resp, err = client.R().
|
||||
SetResult(&imgs).
|
||||
SetQueryParam("images", strconv.FormatInt(image.Id, 10)).
|
||||
Get("http://localhost:8080/api/images")
|
||||
if err != nil || resp.StatusCode() != http.StatusOK {
|
||||
t.Logf("%+v", resp)
|
||||
t.Fatal("failed to get image by image id")
|
||||
}
|
||||
|
||||
if len(imgs) > 0 {
|
||||
t.Fatal("image not deleted")
|
||||
}
|
||||
}
|
||||
BIN
tests/resources/huh.png
Normal file
BIN
tests/resources/huh.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 170 KiB |
Reference in New Issue
Block a user