Skip to content

Commit

Permalink
Http test (#214)
Browse files Browse the repository at this point in the history
* api tests
* fix: code style issues (#215)
* fix: verify chainID of rpc
* feature: #74, share pod with new name (#217)
* fix: insufficient funds error message
  • Loading branch information
asabya authored Aug 1, 2022
1 parent 592ac1a commit 5055d2a
Show file tree
Hide file tree
Showing 115 changed files with 1,807 additions and 3,522 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go: [1.18]
go: [1.17]
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: Setup Go
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GO ?= go
GOLANGCI_LINT ?= $$($(GO) env GOPATH)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.46.1
GOLANGCI_LINT_VERSION ?= v1.47.2
GOGOPROTOBUF ?= protoc-gen-gogofaster
GOGOPROTOBUF_VERSION ?= v1.3.1

Expand Down
7 changes: 4 additions & 3 deletions cmd/common/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ type UserRequest struct {
}

type PodRequest struct {
PodName string `json:"pod_name,omitempty"`
Password string `json:"password,omitempty"`
Reference string `json:"reference,omitempty"`
PodName string `json:"pod_name,omitempty"`
Password string `json:"password,omitempty"`
Reference string `json:"reference,omitempty"`
SharedPodName string `json:"shared_pod_name,omitempty"`
}

type FileSystemRequest struct {
Expand Down
51 changes: 26 additions & 25 deletions cmd/dfs-cli/cmd/fdfs-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/cookiejar"
Expand All @@ -36,23 +35,24 @@ import (
)

const (
MaxIdleConnections = 20
MaxConnectionsPerHost = 256
RequestTimeout = 6000
maxIdleConnections = 20
maxConnectionsPerHost = 256
requestTimeout = 6000
)

type FdfsClient struct {
// fdfsClient is the http client for dfs
type fdfsClient struct {
url string
client *http.Client
cookie *http.Cookie
}

func NewFdfsClient(fdfsServer string) (*FdfsClient, error) {
func newFdfsClient(fdfsServer string) (*fdfsClient, error) {
client, err := createHTTPClient()
if err != nil {
return nil, err
}
return &FdfsClient{
return &fdfsClient{
url: fdfsServer,
client: client,
}, nil
Expand All @@ -64,18 +64,19 @@ func createHTTPClient() (*http.Client, error) {
return nil, err
}
client := &http.Client{
Timeout: time.Second * RequestTimeout,
Timeout: time.Second * requestTimeout,
Jar: jar,
Transport: &http.Transport{
MaxIdleConnsPerHost: MaxIdleConnections,
MaxConnsPerHost: MaxConnectionsPerHost,
MaxIdleConnsPerHost: maxIdleConnections,
MaxConnsPerHost: maxConnectionsPerHost,
},
}
return client, nil
}

func (s *FdfsClient) CheckConnection() bool {
req, err := http.NewRequest(http.MethodGet, s.url, nil)
// CheckConnection checks if it can connect to dfs server
func (s *fdfsClient) CheckConnection() bool {
req, err := http.NewRequest(http.MethodGet, s.url, http.NoBody)
if err != nil {
return false
}
Expand All @@ -91,11 +92,11 @@ func (s *FdfsClient) CheckConnection() bool {
return false
}

_, err = ioutil.ReadAll(response.Body)
_, err = io.ReadAll(response.Body)
return err == nil
}

func (s *FdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte, error) {
func (s *fdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte, error) {
// prepare the request
fullUrl := fmt.Sprintf(s.url + urlPath)
var req *http.Request
Expand All @@ -110,7 +111,7 @@ func (s *FdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte,
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Content-Length", strconv.Itoa(len(jsonBytes)))
} else {
req, err = http.NewRequest(method, fullUrl, nil)
req, err = http.NewRequest(method, fullUrl, http.NoBody)
if err != nil {
return nil, err
}
Expand All @@ -132,7 +133,7 @@ func (s *FdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte,
if response.StatusCode == http.StatusNoContent {
return nil, errors.New("no content")
}
data, err := ioutil.ReadAll(response.Body)
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("error downloading data")
}
Expand All @@ -151,7 +152,7 @@ func (s *FdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte,
s.cookie = response.Cookies()[0]
}

data, err := ioutil.ReadAll(response.Body)
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("error downloading data")
}
Expand All @@ -170,18 +171,18 @@ func (s *FdfsClient) postReq(method, urlPath string, jsonBytes []byte) ([]byte,
return []byte(resp.Message), nil
}

func (s *FdfsClient) getReq(urlPath, argsString string) ([]byte, error) {
func (s *fdfsClient) getReq(urlPath, argsString string) ([]byte, error) {
fullUrl := fmt.Sprintf(s.url + urlPath)
var req *http.Request
var err error
if argsString != "" {
fullUrl = fullUrl + "?" + argsString
req, err = http.NewRequest(http.MethodGet, fullUrl, nil)
req, err = http.NewRequest(http.MethodGet, fullUrl, http.NoBody)
if err != nil {
return nil, err
}
} else {
req, err = http.NewRequest(http.MethodGet, fullUrl, nil)
req, err = http.NewRequest(http.MethodGet, fullUrl, http.NoBody)
if err != nil {
return nil, err
}
Expand All @@ -203,7 +204,7 @@ func (s *FdfsClient) getReq(urlPath, argsString string) ([]byte, error) {
if response.StatusCode == http.StatusNoContent {
return nil, errors.New("no content")
}
data, err := ioutil.ReadAll(response.Body)
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("error downloading data")
}
Expand All @@ -219,7 +220,7 @@ func (s *FdfsClient) getReq(urlPath, argsString string) ([]byte, error) {
s.cookie = response.Cookies()[0]
}

data, err := ioutil.ReadAll(response.Body)
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("error downloading data")
}
Expand All @@ -237,7 +238,7 @@ func (s *FdfsClient) getReq(urlPath, argsString string) ([]byte, error) {
return []byte(resp.Message), nil
}

func (s *FdfsClient) uploadMultipartFile(urlPath, fileName string, fileSize int64, fd *os.File, arguments map[string]string, formFileArgument, compression string) ([]byte, error) {
func (s *fdfsClient) uploadMultipartFile(urlPath, fileName string, fileSize int64, fd *os.File, arguments map[string]string, formFileArgument, compression string) ([]byte, error) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)

Expand Down Expand Up @@ -296,7 +297,7 @@ func (s *FdfsClient) uploadMultipartFile(urlPath, fileName string, fileSize int6
return nil, errors.New(errStr)
}

data, err := ioutil.ReadAll(response.Body)
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.New("error downloading data")
}
Expand All @@ -305,7 +306,7 @@ func (s *FdfsClient) uploadMultipartFile(urlPath, fileName string, fileSize int6

}

func (s *FdfsClient) downloadMultipartFile(method, urlPath string, arguments map[string]string, out *os.File) (int64, error) {
func (s *fdfsClient) downloadMultipartFile(method, urlPath string, arguments map[string]string, out *os.File) (int64, error) {
// prepare the request
fullUrl := fmt.Sprintf(s.url + urlPath)
var req *http.Request
Expand Down
2 changes: 1 addition & 1 deletion cmd/dfs-cli/cmd/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func statFileOrDirectory(podName, statElement string) {
return
}
} else {
var resp dir.DirStats
var resp dir.Stats
err = json.Unmarshal(data, &resp)
if err != nil {
fmt.Println("file stat: ", err)
Expand Down
22 changes: 10 additions & 12 deletions cmd/dfs-cli/cmd/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func podNew(podName string) {
return
}
currentPod = podName
currentDirectory = utils.PathSeperator
currentDirectory = utils.PathSeparator
message := strings.ReplaceAll(string(data), "\n", "")
fmt.Println(message)
}
Expand Down Expand Up @@ -84,14 +84,14 @@ func openPod(podName string) {
}
invalidPodName := true
password := ""
for _, pod := range resp.Pods {
if pod == podName {
for _, v := range resp.Pods {
if v == podName {
password = getPassword()
invalidPodName = false
}
}
for _, pod := range resp.SharedPods {
if pod == podName {
for _, v := range resp.SharedPods {
if v == podName {
invalidPodName = false
}
}
Expand All @@ -115,7 +115,7 @@ func openPod(podName string) {
return
}
currentPod = podName
currentDirectory = utils.PathSeperator
currentDirectory = utils.PathSeparator
message := strings.ReplaceAll(string(data), "\n", "")
fmt.Println(message)
}
Expand Down Expand Up @@ -193,11 +193,11 @@ func listPod() {
fmt.Println("pod list: ", err)
return
}
for _, pod := range resp.Pods {
fmt.Println("<Pod>: ", pod)
for _, v := range resp.Pods {
fmt.Println("<Pod>: ", v)
}
for _, pod := range resp.SharedPods {
fmt.Println("<Shared Pod>: ", pod)
for _, v := range resp.SharedPods {
fmt.Println("<Shared Pod>: ", v)
}
}

Expand Down Expand Up @@ -242,7 +242,5 @@ func receiveInfo(sharingRef string) {
}
fmt.Println("Pod Name : ", podSharingInfo.PodName)
fmt.Println("Pod Ref. : ", podSharingInfo.Address)
fmt.Println("User Name : ", podSharingInfo.UserName)
fmt.Println("User Ref. : ", podSharingInfo.UserAddress)
fmt.Println("Shared Time : ", podSharingInfo.SharedTime)
}
Loading

0 comments on commit 5055d2a

Please sign in to comment.