forked from freeconf/restconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_test.go
122 lines (112 loc) · 2.53 KB
/
form_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package restconf
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"testing"
"time"
"github.com/freeconf/yang/node"
"github.com/freeconf/yang/nodeutil"
"github.com/freeconf/yang/parser"
)
type handlerImpl http.HandlerFunc
func (impl handlerImpl) ServeHTTP(w http.ResponseWriter, r *http.Request) {
impl(w, r)
}
func TestForm(t *testing.T) {
if os.Getenv("TRAVIS") == "true" {
// no web servers allowed in CI
t.Skip()
return
}
m, err := parser.LoadModuleFromString(nil, `
module test {
rpc x {
input {
leaf a {
type string;
}
anydata b;
}
}
}
`)
if err != nil {
t.Fatal(err)
}
done := make(chan bool, 1)
handler := func(w http.ResponseWriter, r *http.Request) {
b := node.NewBrowser(m, formDummyNode(t))
x := m.Actions()["x"]
input, err := readInput(Strict, YangDataJsonMimeType1, r, x)
chkErr(t, err)
xsel, err := b.Root().Find("x")
chkErr(t, err)
_, err = xsel.Action(input)
chkErr(t, err)
w.Write([]byte("ok"))
t.Log("form received")
done <- true
}
srv := &http.Server{Addr: "127.0.0.1:9999", Handler: handlerImpl(handler)}
go srv.ListenAndServe()
defer srv.Shutdown(context.TODO())
// wait for server to start
<-time.After(10 * time.Millisecond)
var buf bytes.Buffer
form := multipart.NewWriter(&buf)
dataPart, err := form.CreateFormField("a")
chkErr(t, err)
fmt.Fprint(dataPart, "hello")
filePart, err := form.CreateFormFile("b", "b")
chkErr(t, err)
fmt.Fprint(filePart, "hello world")
chkErr(t, form.Close())
req, err := http.NewRequest("POST", "http://"+srv.Addr, &buf)
chkErr(t, err)
req.Header.Set("Content-Type", form.FormDataContentType())
_, err = http.DefaultClient.Do(req)
// If you get an error here, make sure something else isn't running on same port
chkErr(t, err)
<-done
}
func chkErr(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
func formDummyNode(t *testing.T) node.Node {
return &nodeutil.Basic{
OnAction: func(r node.ActionRequest) (node.Node, error) {
sel, err := r.Input.Find("a")
chkErr(t, err)
v, err := sel.Get()
chkErr(t, err)
if v.String() != "hello" {
t.Error(v.String())
}
sel, err = r.Input.Find("b")
chkErr(t, err)
v, err = sel.Get()
chkErr(t, err)
rdr, valid := v.Value().(io.Reader)
if !valid {
panic("invalid")
}
actual, err := ioutil.ReadAll(rdr)
chkErr(t, err)
if string(actual) != "hello world" {
t.Error(actual)
}
//defer rdr.Close()
fmt.Print(string(actual))
return nil, nil
},
}
}