mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 04:29:17 +00:00
adding http utility functions
This commit is contained in:
parent
c5cf72000f
commit
491b86fba9
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,6 +4,7 @@
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
*.swp
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"testing"
|
||||
"net/http/httptest"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func TestAddAccount(t *testing.T) {
|
||||
@ -15,12 +14,9 @@ func TestAddAccount(t *testing.T) {
|
||||
r.Header.Add("Authorization","Basic " + auth)
|
||||
w := httptest.NewRecorder()
|
||||
AddNodeAccount(w, r)
|
||||
resp := w.Result()
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
var token string
|
||||
dec.Decode(&token)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("failed to create account")
|
||||
if ReadResponse(w, &token) != nil {
|
||||
t.Errorf("failed to create token");
|
||||
return
|
||||
}
|
||||
|
||||
@ -29,16 +25,9 @@ func TestAddAccount(t *testing.T) {
|
||||
r.Header.Add("Authorization","Bearer " + token)
|
||||
w = httptest.NewRecorder()
|
||||
GetAccountToken(w, r)
|
||||
resp = w.Result()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("invalid token value")
|
||||
return
|
||||
}
|
||||
dec = json.NewDecoder(resp.Body)
|
||||
var tokenType string
|
||||
dec.Decode(&tokenType)
|
||||
if tokenType != "create" {
|
||||
t.Errorf("invalid token type")
|
||||
if ReadResponse(w, &tokenType) != nil {
|
||||
t.Errorf("failed to validate token")
|
||||
return
|
||||
}
|
||||
|
||||
@ -47,14 +36,11 @@ func TestAddAccount(t *testing.T) {
|
||||
r.Header.Add("Authorization","Bearer " + token)
|
||||
w = httptest.NewRecorder()
|
||||
GetAccountUsername(w, r)
|
||||
resp = w.Result()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("invalid token value")
|
||||
var available bool
|
||||
if ReadResponse(w, &available) != nil {
|
||||
t.Errorf("failed to check username")
|
||||
return
|
||||
}
|
||||
dec = json.NewDecoder(resp.Body)
|
||||
var available bool
|
||||
dec.Decode(&available)
|
||||
if !available {
|
||||
t.Errorf("username not available")
|
||||
return
|
||||
@ -67,16 +53,9 @@ func TestAddAccount(t *testing.T) {
|
||||
r.Header.Add("Authorization","Bearer " + token)
|
||||
w = httptest.NewRecorder()
|
||||
AddAccount(w, r)
|
||||
resp = w.Result()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("invalid token value")
|
||||
return
|
||||
}
|
||||
dec = json.NewDecoder(resp.Body)
|
||||
var profile Profile
|
||||
dec.Decode(&profile)
|
||||
if profile.Handle != "user" {
|
||||
t.Errorf("invalid profile")
|
||||
if ReadResponse(w, &profile) != nil {
|
||||
t.Errorf("failed to create account")
|
||||
return
|
||||
}
|
||||
|
||||
@ -86,40 +65,33 @@ func TestAddAccount(t *testing.T) {
|
||||
r.Header.Add("Authorization","Basic " + auth)
|
||||
w = httptest.NewRecorder()
|
||||
AddNodeAccount(w, r)
|
||||
resp = w.Result()
|
||||
dec = json.NewDecoder(resp.Body)
|
||||
dec.Decode(&token)
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("failed to create account")
|
||||
if ReadResponse(w, &token) != nil {
|
||||
t.Errorf("failed to create token")
|
||||
return
|
||||
}
|
||||
|
||||
// check if username is available
|
||||
// check if dup is available
|
||||
r = httptest.NewRequest("GET", "/account/claimable?username=user", nil)
|
||||
r.Header.Add("Authorization","Bearer " + token)
|
||||
w = httptest.NewRecorder()
|
||||
GetAccountUsername(w, r)
|
||||
resp = w.Result()
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("invalid token value")
|
||||
if ReadResponse(w, &available) != nil {
|
||||
t.Errorf("failed to check username")
|
||||
return
|
||||
}
|
||||
dec = json.NewDecoder(resp.Body)
|
||||
dec.Decode(&available)
|
||||
if available {
|
||||
t.Errorf("username duplicate available")
|
||||
return
|
||||
}
|
||||
|
||||
// create account
|
||||
// create dup account
|
||||
auth = base64.StdEncoding.EncodeToString([]byte("user:pass"))
|
||||
r = httptest.NewRequest("GET", "/account/profile", nil)
|
||||
r.Header.Add("Credentials","Basic " + auth)
|
||||
r.Header.Add("Authorization","Bearer " + token)
|
||||
w = httptest.NewRecorder()
|
||||
AddAccount(w, r)
|
||||
resp = w.Result()
|
||||
if resp.StatusCode == 200 {
|
||||
if ReadResponse(w, &profile) == nil {
|
||||
t.Errorf("duplicate handle set")
|
||||
return
|
||||
}
|
||||
|
64
net/server/internal/httpUtil.go
Normal file
64
net/server/internal/httpUtil.go
Normal file
@ -0,0 +1,64 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"errors"
|
||||
"encoding/json"
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
func WriteResponse(w http.ResponseWriter, v interface{}) {
|
||||
body, err := json.Marshal(v);
|
||||
if err != nil {
|
||||
LogMsg("marshal failed")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
} else {
|
||||
w.Write(body);
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func ReadResponse(w *httptest.ResponseRecorder, v interface{}) error {
|
||||
resp := w.Result()
|
||||
if resp.StatusCode != 200 {
|
||||
return errors.New("response failed");
|
||||
}
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
dec.Decode(v)
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewRequest(rest string, path string, obj interface{}) (*http.Request, *httptest.ResponseRecorder, error) {
|
||||
w := httptest.NewRecorder()
|
||||
if(obj != nil) {
|
||||
body, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reader := strings.NewReader(string(body))
|
||||
return httptest.NewRequest(rest, path, reader), w, nil
|
||||
}
|
||||
|
||||
return httptest.NewRequest(rest, path, nil), w, nil
|
||||
}
|
||||
|
||||
func SetBasicAuth(r *http.Request, login string) {
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(login))
|
||||
r.Header.Add("Authorization", "Basic " + auth)
|
||||
}
|
||||
|
||||
func SetBearerAuth(r *http.Request, token string) {
|
||||
r.Header.Add("Authorization", "Bearer " + token)
|
||||
}
|
||||
|
||||
func SetCredentials(r *http.Request, login string) {
|
||||
auth := base64.StdEncoding.EncodeToString([]byte(login))
|
||||
r.Header.Add("Credentials", "Basic " + auth)
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ import (
|
||||
"github.com/kr/pretty"
|
||||
)
|
||||
|
||||
var hideLog bool = false
|
||||
|
||||
func Logger(inner http.Handler, name string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
@ -36,9 +38,11 @@ func Logger(inner http.Handler, name string) http.Handler {
|
||||
}
|
||||
|
||||
func LogMsg(msg string) {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
p, _ := os.Getwd()
|
||||
log.Printf("%s:%d %s", strings.TrimPrefix(file, p), line, msg)
|
||||
if !hideLog {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
p, _ := os.Getwd()
|
||||
log.Printf("%s:%d %s", strings.TrimPrefix(file, p), line, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func PrintMsg(obj interface{}) {
|
||||
|
@ -1,16 +1,13 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"net/http/httptest"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"databag/internal/store"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
hideLog = true
|
||||
store.SetPath("file::memory:?cache=shared");
|
||||
//store.SetPath("databag.db");
|
||||
|
||||
@ -24,59 +21,39 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
|
||||
func Claimable() {
|
||||
r := httptest.NewRequest("GET", "/admin/claimable", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r, w, _ := NewRequest("GET", "/admin/claimable", nil)
|
||||
GetNodeClaimable(w, r)
|
||||
|
||||
//body, _ := ioutil.ReadAll(resp.Body)
|
||||
resp := w.Result()
|
||||
dec := json.NewDecoder(resp.Body);
|
||||
var res bool
|
||||
err := dec.Decode(&res)
|
||||
if err != nil {
|
||||
panic("failed to get claimable response")
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
panic("server not initially claimable")
|
||||
var available bool
|
||||
if ReadResponse(w, &available) != nil {
|
||||
panic("server not claimable")
|
||||
}
|
||||
}
|
||||
|
||||
func Claim() {
|
||||
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
|
||||
r := httptest.NewRequest("PUT", "/admin/claim", nil)
|
||||
r.Header.Add("Credentials","Basic " + auth)
|
||||
w := httptest.NewRecorder()
|
||||
r, w, _ := NewRequest("PUT", "/admin/claim", nil)
|
||||
SetCredentials(r, "admin:pass");
|
||||
SetNodeClaim(w, r)
|
||||
if w.Code != 200 {
|
||||
panic("server not initially claimable")
|
||||
if ReadResponse(w, nil) != nil {
|
||||
panic("failed to claim server")
|
||||
}
|
||||
}
|
||||
|
||||
func SetConfig() {
|
||||
config := NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096}
|
||||
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
|
||||
body,_ := json.Marshal(config)
|
||||
r := httptest.NewRequest("PUT", "/admin/config", strings.NewReader(string(body)))
|
||||
r.Header.Add("Authorization","Basic " + auth)
|
||||
w := httptest.NewRecorder()
|
||||
SetNodeConfig(w, r);
|
||||
if w.Code != 200 {
|
||||
panic("failed to set node config")
|
||||
r, w, _ := NewRequest("PUT", "/admin/config", &config)
|
||||
SetBasicAuth(r, "admin:pass")
|
||||
SetNodeConfig(w, r)
|
||||
if ReadResponse(w, nil) != nil {
|
||||
panic("failed to set config")
|
||||
}
|
||||
}
|
||||
|
||||
func GetConfig() {
|
||||
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
|
||||
r := httptest.NewRequest("GET", "/admin/config", nil)
|
||||
r.Header.Add("Authorization","Basic " + auth)
|
||||
w := httptest.NewRecorder()
|
||||
GetNodeConfig(w, r);
|
||||
|
||||
resp := w.Result();
|
||||
dec := json.NewDecoder(resp.Body);
|
||||
var config NodeConfig;
|
||||
dec.Decode(&config);
|
||||
if resp.StatusCode != 200 {
|
||||
r, w, _ := NewRequest("GET", "/admin/config", nil)
|
||||
SetBasicAuth(r, "admin:pass")
|
||||
GetNodeConfig(w, r)
|
||||
var config NodeConfig
|
||||
if ReadResponse(w, &config) != nil {
|
||||
panic("failed to get node config")
|
||||
}
|
||||
if config.Domain != "example.com" {
|
||||
@ -91,30 +68,22 @@ func GetConfig() {
|
||||
}
|
||||
|
||||
func SetToken() string {
|
||||
auth := base64.StdEncoding.EncodeToString([]byte("admin:pass"))
|
||||
r := httptest.NewRequest("POST", "/admin/accounts", nil)
|
||||
r.Header.Add("Authorization","Basic " + auth)
|
||||
w := httptest.NewRecorder()
|
||||
r, w, _ := NewRequest("POST", "/admin/accounts", nil)
|
||||
SetBasicAuth(r, "admin:pass")
|
||||
AddNodeAccount(w, r)
|
||||
resp := w.Result()
|
||||
dec := json.NewDecoder(resp.Body)
|
||||
var token string
|
||||
dec.Decode(&token)
|
||||
if resp.StatusCode != 200 {
|
||||
if ReadResponse(w, &token) != nil {
|
||||
panic("failed to create token")
|
||||
}
|
||||
return token
|
||||
}
|
||||
|
||||
func SetAccount(token string) {
|
||||
auth := base64.StdEncoding.EncodeToString([]byte("test:pass"))
|
||||
r := httptest.NewRequest("GET", "/account/profile", nil)
|
||||
r.Header.Add("Credentials","Basic " + auth)
|
||||
r.Header.Add("Authorization","Bearer " + token)
|
||||
w := httptest.NewRecorder()
|
||||
r, w, _ := NewRequest("GET", "/account/profile", nil)
|
||||
SetBearerAuth(r, token);
|
||||
SetCredentials(r, "test:pass")
|
||||
AddAccount(w, r)
|
||||
resp := w.Result()
|
||||
if resp.StatusCode != 200 {
|
||||
if ReadResponse(w, nil) != nil {
|
||||
panic("failed to create account")
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +0,0 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func WriteResponse(w http.ResponseWriter, v interface{}) {
|
||||
body, err := json.Marshal(v);
|
||||
if err != nil {
|
||||
LogMsg("marshal failed")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
} else {
|
||||
w.Write(body);
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user