mirror of
https://github.com/simon987/task_tracker.git
synced 2025-12-14 07:19:02 +00:00
Initial commit
This commit is contained in:
53
test/common.go
Normal file
53
test/common.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"src/task_tracker/config"
|
||||
)
|
||||
|
||||
func Post(path string, x interface{}) *http.Response {
|
||||
|
||||
body, err := json.Marshal(x)
|
||||
buf := bytes.NewBuffer(body)
|
||||
|
||||
r, err := http.Post("http://" + config.Cfg.ServerAddr + path, "application/json", buf)
|
||||
handleErr(err)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func Get(path string) *http.Response {
|
||||
r, err := http.Get("http://" + config.Cfg.ServerAddr + path)
|
||||
handleErr(err)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
func handleErr(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Print(body io.ReadCloser) {
|
||||
rawBody, _ := ioutil.ReadAll(body)
|
||||
fmt.Println(string(rawBody))
|
||||
}
|
||||
|
||||
func GenericJson(body io.ReadCloser) map[string]interface{} {
|
||||
|
||||
var obj map[string]interface{}
|
||||
|
||||
data, _ := ioutil.ReadAll(body)
|
||||
|
||||
err := json.Unmarshal(data, &obj)
|
||||
handleErr(err)
|
||||
|
||||
return obj
|
||||
}
|
||||
Reference in New Issue
Block a user