-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* DeleteHandler * func Test_PlainDownloadHandler(t *testing.T) { * Test_UploadHandler * Test_KeyPairHandler
- Loading branch information
Showing
5 changed files
with
599 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.