mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-25 22:36:05 +03:00
test http post
This commit is contained in:
parent
f7dd9679d3
commit
ec6c90e395
46
playground/http/postyamlstring_test.go
Normal file
46
playground/http/postyamlstring_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPostYamlString(t *testing.T) {
|
||||
var filePath = "/Users/bruce/Desktop/HVAC-CoolMasterNet.yml"
|
||||
var url = "http://localhost:48081/api/v1/deviceprofile/upload"
|
||||
|
||||
// read file to byte
|
||||
yamlFile, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(yamlFile))
|
||||
|
||||
// create http post request
|
||||
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(yamlFile))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// submit request
|
||||
client := &http.Client{}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// check response
|
||||
fmt.Println("== upload finish ==")
|
||||
resBody, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println(res.StatusCode)
|
||||
fmt.Println(res.Header)
|
||||
res.Body.Close()
|
||||
fmt.Println(string(resBody))
|
||||
}
|
63
playground/http/upload_test.go
Normal file
63
playground/http/upload_test.go
Normal file
@ -0,0 +1,63 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUploadFormFile(t *testing.T) {
|
||||
var filePath = "/Users/bruce/Desktop/HVAC-CoolMasterNet.yml"
|
||||
var url = "http://localhost:48081/api/v1/deviceprofile/uploadfile"
|
||||
|
||||
// fetch file
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// create form data
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
fmt.Println(filepath.Base(file.Name()))
|
||||
formFile, err := writer.CreateFormFile("file", filepath.Base(file.Name()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = io.Copy(formFile, file); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writer.Close()
|
||||
|
||||
// create http post request
|
||||
req, err := http.NewRequest(http.MethodPost, url, &body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Add("Content-Type", writer.FormDataContentType())
|
||||
|
||||
// submit request
|
||||
client := &http.Client{}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// check response
|
||||
fmt.Println("== upload finish ==")
|
||||
resBody, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println(res.StatusCode)
|
||||
fmt.Println(res.Header)
|
||||
res.Body.Close()
|
||||
fmt.Println(string(resBody))
|
||||
}
|
20
playground/pointer/pointer_test.go
Normal file
20
playground/pointer/pointer_test.go
Normal file
@ -0,0 +1,20 @@
|
||||
package pointer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBasic(t *testing.T) {
|
||||
answer := 42
|
||||
fmt.Println(&answer) // & is address operator
|
||||
|
||||
address := &answer
|
||||
fmt.Println(*address) // * is dereferencing, which providers the value that a memory address refers to.
|
||||
fmt.Printf("address is a %T \n", address) // print the pointer type
|
||||
|
||||
var address2 *int // declare a pointer
|
||||
address2 = address // address2 can store some pinter type
|
||||
fmt.Println(*address2)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user