test(singleuser): add tests for singleuser apis

This commit is contained in:
hexxa 2020-12-06 14:32:45 +08:00
parent 2bcb337b4c
commit 24adbcbe63
11 changed files with 265 additions and 82 deletions

View file

@ -8,24 +8,24 @@ import (
"github.com/parnurzeal/gorequest"
)
type QSClient struct {
type FilesClient struct {
addr string
r *gorequest.SuperAgent
}
func NewQSClient(addr string) *QSClient {
func NewFilesClient(addr string) *FilesClient {
gr := gorequest.New()
return &QSClient{
return &FilesClient{
addr: addr,
r: gr,
}
}
func (cl *QSClient) url(urlpath string) string {
func (cl *FilesClient) url(urlpath string) string {
return fmt.Sprintf("%s%s", cl.addr, urlpath)
}
func (cl *QSClient) Create(filepath string, size int64) (gorequest.Response, string, []error) {
func (cl *FilesClient) Create(filepath string, size int64) (gorequest.Response, string, []error) {
return cl.r.Post(cl.url("/v1/fs/files")).
Send(fileshdr.CreateReq{
Path: filepath,
@ -34,13 +34,13 @@ func (cl *QSClient) Create(filepath string, size int64) (gorequest.Response, str
End()
}
func (cl *QSClient) Delete(filepath string) (gorequest.Response, string, []error) {
func (cl *FilesClient) Delete(filepath string) (gorequest.Response, string, []error) {
return cl.r.Delete(cl.url("/v1/fs/files")).
Param(fileshdr.FilePathQuery, filepath).
End()
}
func (cl *QSClient) Metadata(filepath string) (gorequest.Response, *fileshdr.MetadataResp, []error) {
func (cl *FilesClient) Metadata(filepath string) (gorequest.Response, *fileshdr.MetadataResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/fs/metadata")).
Param(fileshdr.FilePathQuery, filepath).
End()
@ -54,13 +54,13 @@ func (cl *QSClient) Metadata(filepath string) (gorequest.Response, *fileshdr.Met
return resp, mResp, nil
}
func (cl *QSClient) Mkdir(dirpath string) (gorequest.Response, string, []error) {
func (cl *FilesClient) Mkdir(dirpath string) (gorequest.Response, string, []error) {
return cl.r.Post(cl.url("/v1/fs/dirs")).
Send(fileshdr.MkdirReq{Path: dirpath}).
End()
}
func (cl *QSClient) Move(oldpath, newpath string) (gorequest.Response, string, []error) {
func (cl *FilesClient) Move(oldpath, newpath string) (gorequest.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/fs/files/move")).
Send(fileshdr.MoveReq{
OldPath: oldpath,
@ -69,7 +69,7 @@ func (cl *QSClient) Move(oldpath, newpath string) (gorequest.Response, string, [
End()
}
func (cl *QSClient) UploadChunk(filepath string, content string, offset int64) (gorequest.Response, string, []error) {
func (cl *FilesClient) UploadChunk(filepath string, content string, offset int64) (gorequest.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/fs/files/chunks")).
Send(fileshdr.UploadChunkReq{
Path: filepath,
@ -79,7 +79,7 @@ func (cl *QSClient) UploadChunk(filepath string, content string, offset int64) (
End()
}
func (cl *QSClient) UploadStatus(filepath string) (gorequest.Response, *fileshdr.UploadStatusResp, []error) {
func (cl *FilesClient) UploadStatus(filepath string) (gorequest.Response, *fileshdr.UploadStatusResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/fs/files/chunks")).
Param(fileshdr.FilePathQuery, filepath).
End()
@ -93,7 +93,7 @@ func (cl *QSClient) UploadStatus(filepath string) (gorequest.Response, *fileshdr
return resp, uResp, nil
}
func (cl *QSClient) Download(filepath string, headers map[string]string) (gorequest.Response, string, []error) {
func (cl *FilesClient) Download(filepath string, headers map[string]string) (gorequest.Response, string, []error) {
r := cl.r.Get(cl.url("/v1/fs/files/chunks")).
Param(fileshdr.FilePathQuery, filepath)
for key, val := range headers {
@ -102,7 +102,7 @@ func (cl *QSClient) Download(filepath string, headers map[string]string) (gorequ
return r.End()
}
func (cl *QSClient) List(dirPath string) (gorequest.Response, *fileshdr.ListResp, []error) {
func (cl *FilesClient) List(dirPath string) (gorequest.Response, *fileshdr.ListResp, []error) {
resp, body, errs := cl.r.Get(cl.url("/v1/fs/dirs")).
Param(fileshdr.ListDirQuery, dirPath).
End()

55
src/client/singleuser.go Normal file
View file

@ -0,0 +1,55 @@
package client
import (
"fmt"
"net/http"
su "github.com/ihexxa/quickshare/src/handlers/singleuserhdr"
"github.com/parnurzeal/gorequest"
)
type SingleUserClient struct {
addr string
r *gorequest.SuperAgent
}
func NewSingleUserClient(addr string) *SingleUserClient {
gr := gorequest.New()
return &SingleUserClient{
addr: addr,
r: gr,
}
}
func (cl *SingleUserClient) url(urlpath string) string {
return fmt.Sprintf("%s%s", cl.addr, urlpath)
}
func (cl *SingleUserClient) Login(user, pwd string) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/login")).
Send(su.LoginReq{
User: user,
Pwd: pwd,
}).
End()
}
func (cl *SingleUserClient) Logout(user string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/logout")).
Send(su.LogoutReq{
User: user,
}).
AddCookie(token).
End()
}
func (cl *SingleUserClient) SetPwd(user, oldPwd, newPwd string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/pwd")).
Send(su.SetPwdReq{
User: user,
OldPwd: oldPwd,
NewPwd: newPwd,
}).
AddCookie(token).
End()
}

12
src/client/utils.go Normal file
View file

@ -0,0 +1,12 @@
package client
import "net/http"
func GetCookie(cookies []*http.Cookie, name string) *http.Cookie {
for _, c := range cookies {
if c.Name == name {
return c
}
}
return nil
}