Skip to content

Commit

Permalink
Http handler tests (#17)
Browse files Browse the repository at this point in the history
* DeleteHandler

* func Test_PlainDownloadHandler(t *testing.T) {

* Test_UploadHandler

* Test_KeyPairHandler
  • Loading branch information
dhcgn authored Jun 21, 2024
1 parent c65c403 commit 4a1c7c3
Show file tree
Hide file tree
Showing 5 changed files with 599 additions and 1 deletion.
3 changes: 2 additions & 1 deletion httphandler/deleteHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ func (c Config) DeleteHandler(w http.ResponseWriter, r *http.Request) {
return
}

// TODO return NOT FOUND if key does not exist?
if err := c.StorageInstance.Delete(downloadKey); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
w.Write([]byte("OK\n"))
}
109 changes: 109 additions & 0 deletions httphandler/deleteHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package httphandler

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/dhcgn/iot-ephemeral-value-store/stats"
"github.com/dhcgn/iot-ephemeral-value-store/storage"
"github.com/gorilla/mux"
)

func Test_DeleteHandler(t *testing.T) {
type args struct {
w http.ResponseWriter
r *http.Request
}
tests := []struct {
name string
c Config
args args
expectedStatus int
expectedBody string
expectedHTTPErrorCount int
}{
{
name: "DeleteHandler - invalid upload key",
c: Config{
StatsInstance: stats.NewStats(),
StorageInstance: storage.NewInMemoryStorage(),
},
args: args{
w: httptest.NewRecorder(),
r: func() *http.Request {
req, _ := http.NewRequest("DELETE", "/delete/invalidUploadKey", nil)
vars := map[string]string{
"uploadKey": "invalidUploadKey",
}
return mux.SetURLVars(req, vars)
}(),
},
expectedStatus: http.StatusInternalServerError,
expectedBody: "Error deriving download key\n",
expectedHTTPErrorCount: 1,
},
{
name: "DeleteHandler - unknown upload key",
c: Config{
StatsInstance: stats.NewStats(),
StorageInstance: storage.NewInMemoryStorage(),
},
args: args{
w: httptest.NewRecorder(),
r: func() *http.Request {
req, _ := http.NewRequest("DELETE", "/delete/0143a8a24c3b364ce4df085579601d9f2408f5e93f851078b3f5e4088eb13220", nil)
vars := map[string]string{
"uploadKey": "0143a8a24c3b364ce4df085579601d9f2408f5e93f851078b3f5e4088eb13220",
}
return mux.SetURLVars(req, vars)
}(),
},
expectedStatus: http.StatusOK,
expectedBody: "OK\n",
expectedHTTPErrorCount: 0,
},
{
name: "DeleteHandler - known upload key",
c: Config{
StatsInstance: stats.NewStats(),
StorageInstance: func() storage.Storage {
storageInstance := storage.NewInMemoryStorage()
_ = storageInstance.Store("7790e6a7c72e97c2493334f7b22ffbaa2a41fc53a95268a4fbb45a9c34d9c5d1", map[string]interface{}{"key": "value"})
return storageInstance
}(),
},
args: args{
w: httptest.NewRecorder(),
r: func() *http.Request {
req, _ := http.NewRequest("DELETE", "/delete/f3749e7288bac3cda9a739f3525da4cc883037e57a984046d5f42d160368078a", nil)
vars := map[string]string{
"uploadKey": "f3749e7288bac3cda9a739f3525da4cc883037e57a984046d5f42d160368078a",
}
return mux.SetURLVars(req, vars)
}(),
},
expectedStatus: http.StatusOK,
expectedBody: "OK\n",
expectedHTTPErrorCount: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.c.DeleteHandler(tt.args.w, tt.args.r)

resp := tt.args.w.(*httptest.ResponseRecorder)
if resp.Code != tt.expectedStatus {
t.Errorf("DeleteHandler returned wrong status code: got \"%v\" want \"%v\"", resp.Code, tt.expectedStatus)
}

if resp.Body.String() != tt.expectedBody {
t.Errorf("DeleteHandler returned unexpected body: got \"%v\" want \"%v\"", resp.Body.String(), tt.expectedBody)
}

if tt.c.StatsInstance.GetCurrentStats().HTTPErrorCount != tt.expectedHTTPErrorCount {
t.Errorf("DeleteHandler did not increment HTTPErrorCount: got \"%v\" want \"%v\"", tt.c.StatsInstance.GetCurrentStats().HTTPErrorCount, tt.expectedHTTPErrorCount)
}
})
}
}
214 changes: 214 additions & 0 deletions httphandler/downloadHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package httphandler

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/dhcgn/iot-ephemeral-value-store/stats"
"github.com/dhcgn/iot-ephemeral-value-store/storage"
"github.com/gorilla/mux"
)

func Test_PlainDownloadHandler(t *testing.T) {
type args struct {
w http.ResponseWriter
r *http.Request
}
tests := []struct {
name string
c Config
args args
expectedStatus int
expectedBody string
expectedHTTPErrorCount int
expectedDownloadCount int
}{
{
name: "PlainDownloadHandler - invalid download key",
c: Config{
StatsInstance: stats.NewStats(),
StorageInstance: storage.NewInMemoryStorage(),
},
args: args{
w: httptest.NewRecorder(),
r: func() *http.Request {
req, _ := http.NewRequest("GET", "/download/invalidDownloadKey/param", nil)
vars := map[string]string{
"downloadKey": "invalidDownloadKey",
"param": "param",
}
return mux.SetURLVars(req, vars)
}(),
},
expectedStatus: http.StatusNotFound,
expectedBody: "Invalid download key or database error\n",
expectedHTTPErrorCount: 1,
expectedDownloadCount: 0,
},
{
name: "PlainDownloadHandler - valid download key, invalid param",
c: Config{
StatsInstance: stats.NewStats(),
StorageInstance: func() storage.Storage {
s := storage.NewInMemoryStorage()
data := map[string]interface{}{"key": "value"}
s.Store("validKey", data)
return s
}(),
},
args: args{
w: httptest.NewRecorder(),
r: func() *http.Request {
req, _ := http.NewRequest("GET", "/download/validKey/invalidParam", nil)
vars := map[string]string{
"downloadKey": "validKey",
"param": "invalidParam",
}
return mux.SetURLVars(req, vars)
}(),
},
expectedStatus: http.StatusNotFound,
expectedBody: "Parameter not found\n",
expectedHTTPErrorCount: 1,
expectedDownloadCount: 0,
},
{
name: "PlainDownloadHandler - valid download key and param",
c: Config{
StatsInstance: stats.NewStats(),
StorageInstance: func() storage.Storage {
s := storage.NewInMemoryStorage()
data := map[string]interface{}{"key": "value"}
s.Store("validKey", data)
return s
}(),
},
args: args{
w: httptest.NewRecorder(),
r: func() *http.Request {
req, _ := http.NewRequest("GET", "/download/validKey/key", nil)
vars := map[string]string{
"downloadKey": "validKey",
"param": "key",
}
return mux.SetURLVars(req, vars)
}(),
},
expectedStatus: http.StatusOK,
expectedBody: "value\n",
expectedHTTPErrorCount: 0,
expectedDownloadCount: 1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.c.PlainDownloadHandler(tt.args.w, tt.args.r)

resp := tt.args.w.(*httptest.ResponseRecorder)
if resp.Code != tt.expectedStatus {
t.Errorf("PlainDownloadHandler returned wrong status code: got \"%v\" want \"%v\"", resp.Code, tt.expectedStatus)
}

if resp.Body.String() != tt.expectedBody {
t.Errorf("PlainDownloadHandler returned unexpected body: got \"%v\" want \"%v\"", resp.Body.String(), tt.expectedBody)
}

if tt.c.StatsInstance.GetCurrentStats().HTTPErrorCount != tt.expectedHTTPErrorCount {
t.Errorf("PlainDownloadHandler did not increment HTTPErrorCount correctly: got \"%v\" want \"%v\"", tt.c.StatsInstance.GetCurrentStats().HTTPErrorCount, tt.expectedHTTPErrorCount)
}

if tt.c.StatsInstance.GetCurrentStats().DownloadCount != tt.expectedDownloadCount {
t.Errorf("PlainDownloadHandler did not increment DownloadCount correctly: got \"%v\" want \"%v\"", tt.c.StatsInstance.GetCurrentStats().DownloadCount, tt.expectedDownloadCount)
}
})
}
}

func Test_DownloadHandler(t *testing.T) {
type args struct {
w http.ResponseWriter
r *http.Request
}
tests := []struct {
name string
c Config
args args
expectedStatus int
expectedBody string
expectedHTTPErrorCount int
expectedDownloadCount int
}{
{
name: "DownloadHandler - invalid download key",
c: Config{
StatsInstance: stats.NewStats(),
StorageInstance: storage.NewInMemoryStorage(),
},
args: args{
w: httptest.NewRecorder(),
r: func() *http.Request {
req, _ := http.NewRequest("GET", "/download/invalidDownloadKey", nil)
vars := map[string]string{
"downloadKey": "invalidDownloadKey",
}
return mux.SetURLVars(req, vars)
}(),
},
expectedStatus: http.StatusNotFound,
expectedBody: "Invalid download key or database error\n",
expectedHTTPErrorCount: 1,
expectedDownloadCount: 0,
},
{
name: "DownloadHandler - valid download key",
c: Config{
StatsInstance: stats.NewStats(),
StorageInstance: func() storage.Storage {
s := storage.NewInMemoryStorage()
data := map[string]interface{}{"key": "value"}
s.Store("validKey", data)
return s
}(),
},
args: args{
w: httptest.NewRecorder(),
r: func() *http.Request {
req, _ := http.NewRequest("GET", "/download/validKey", nil)
vars := map[string]string{
"downloadKey": "validKey",
}
return mux.SetURLVars(req, vars)
}(),
},
expectedStatus: http.StatusOK,
expectedBody: `{"key":"value"}`,
expectedHTTPErrorCount: 0,
expectedDownloadCount: 1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.c.DownloadHandler(tt.args.w, tt.args.r)

resp := tt.args.w.(*httptest.ResponseRecorder)
if resp.Code != tt.expectedStatus {
t.Errorf("DownloadHandler returned wrong status code: got \"%v\" want \"%v\"", resp.Code, tt.expectedStatus)
}

if resp.Body.String() != tt.expectedBody {
t.Errorf("DownloadHandler returned unexpected body: got \"%v\" want \"%v\"", resp.Body.String(), tt.expectedBody)
}

if tt.c.StatsInstance.GetCurrentStats().HTTPErrorCount != tt.expectedHTTPErrorCount {
t.Errorf("DownloadHandler did not increment HTTPErrorCount correctly: got \"%v\" want \"%v\"", tt.c.StatsInstance.GetCurrentStats().HTTPErrorCount, tt.expectedHTTPErrorCount)
}

if tt.c.StatsInstance.GetCurrentStats().DownloadCount != tt.expectedDownloadCount {
t.Errorf("DownloadHandler did not increment DownloadCount correctly: got \"%v\" want \"%v\"", tt.c.StatsInstance.GetCurrentStats().DownloadCount, tt.expectedDownloadCount)
}
})
}
}
Loading

0 comments on commit 4a1c7c3

Please sign in to comment.