Add some tests for http server
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
1caec97d81
commit
d8817cc67f
@ -78,10 +78,11 @@ func ActionUpload(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
mw.Close()
|
mw.Close()
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, buf)
|
||||||
req.Header.Add("Content-Type", mw.FormDataContentType())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
req.Header.Add("Content-Type", mw.FormDataContentType())
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
99
http_test.go
Normal file
99
http_test.go
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package gpaste_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.t-juice.club/torjus/gpaste"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHandlers(t *testing.T) {
|
||||||
|
cfg := &gpaste.ServerConfig{
|
||||||
|
Store: &gpaste.ServerStoreConfig{
|
||||||
|
Type: "memory",
|
||||||
|
},
|
||||||
|
URL: "http://localhost:8080",
|
||||||
|
}
|
||||||
|
hs := gpaste.NewHTTPServer(cfg)
|
||||||
|
|
||||||
|
t.Run("HandlerIndex", func(t *testing.T) {
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
|
||||||
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if status := rr.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("Returned unexpected status")
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedBody := "index"
|
||||||
|
if body := rr.Body.String(); body != expectedBody {
|
||||||
|
t.Errorf("Body does not match expected. Got %s want %s", body, expectedBody)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("HandlerAPIFilePost", func(t *testing.T) {
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
mw := multipart.NewWriter(buf)
|
||||||
|
fw, err := mw.CreateFormFile("test", "test.txt")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to create form file: %s", err)
|
||||||
|
}
|
||||||
|
expectedData := "Test OMEGALUL PLS."
|
||||||
|
if _, err := io.WriteString(fw, expectedData); err != nil {
|
||||||
|
t.Fatalf("Unable to write body to buffer: %s", err)
|
||||||
|
}
|
||||||
|
mw.Close()
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/api/file", buf)
|
||||||
|
req.Header.Add("Content-Type", mw.FormDataContentType())
|
||||||
|
|
||||||
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if status := rr.Code; status != http.StatusAccepted {
|
||||||
|
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
|
||||||
|
}
|
||||||
|
|
||||||
|
var expectedResp []struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
decoder := json.NewDecoder(rr.Result().Body)
|
||||||
|
if err := decoder.Decode(&expectedResp); err != nil {
|
||||||
|
t.Fatalf("error decoding response: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if l := len(expectedResp); l != 1 {
|
||||||
|
t.Errorf("Response has wrong length. Got %d want %d", l, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
uploadID := expectedResp[0].ID
|
||||||
|
if uploadID == "" {
|
||||||
|
t.Errorf("Response has empty id")
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("HandlerAPIFileGet", func(t *testing.T) {
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
url := fmt.Sprintf("/api/file/%s", uploadID)
|
||||||
|
req := httptest.NewRequest(http.MethodGet, url, nil)
|
||||||
|
|
||||||
|
hs.Handler.ServeHTTP(rr, req)
|
||||||
|
|
||||||
|
if status := rr.Code; status != http.StatusOK {
|
||||||
|
t.Errorf("Returned unexpected status. Got %d want %d", status, http.StatusAccepted)
|
||||||
|
t.Logf(url)
|
||||||
|
}
|
||||||
|
if body := rr.Body.String(); body != expectedData {
|
||||||
|
t.Errorf("Returned body does not match expected.")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user