forked from hyperboloide/pdfgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
114 lines (97 loc) · 3.08 KB
/
main_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
package main_test
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/viper"
. "github.com/hyperboloide/pdfgen"
)
var _ = Describe("Main", func() {
var srv *httptest.Server
var docSize int
var js []byte
_ = BeforeEach(func() {
viper.Set("templates", "./templates")
err := ConfigRead()
Expect(err).ToNot(HaveOccurred())
srv = httptest.NewServer(Router())
tmp, err := ioutil.ReadFile("./templates/demo.json")
Expect(err).To(BeNil())
js = tmp
})
_ = AfterEach(func() {
srv.Close()
})
It("should respond StatusMethodNotAllowed to GET requests", func() {
resp, err := http.Get(srv.URL + "/invoice")
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusMethodNotAllowed))
})
It("should respond StatusMethodNotAllowed is content type is not application/json", func() {
resp, err := http.Post(srv.URL+"/invoice", "text/plain", bytes.NewReader(js))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
})
It("should respond StatusNotFound if the template do not exists", func() {
resp, err := http.Post(srv.URL+"/not_found", "application/json", bytes.NewReader(js))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusNotFound))
})
It("should respond StatusBadRequest json is invalid", func() {
resp, err := http.Post(srv.URL+"/invoice", "application/json", strings.NewReader("invalid!!!"))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusBadRequest))
})
It("should make a valid post", func() {
resp, err := http.Post(srv.URL+"/invoice", "application/json", bytes.NewReader(js))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))
b, err := ioutil.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
docSize = len(b)
})
It("should work with appended slash", func() {
resp, err := http.Post(srv.URL+"/invoice/", "application/json", bytes.NewReader(js))
Expect(err).ToNot(HaveOccurred())
Expect(resp.StatusCode).To(Equal(http.StatusOK))
b, err := ioutil.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
Expect(len(b)).To(Equal(docSize))
})
// It("should be able to run in parallel", func() {
//
// var test = func() pool.WorkFunc {
// return func(wu pool.WorkUnit) (interface{}, error) {
// resp, err := http.Post(srv.URL+"/invoice", "application/json", bytes.NewReader(js))
// if err != nil || resp.StatusCode != http.StatusOK {
// return false, err
// } else if b, err := ioutil.ReadAll(resp.Body); err != nil {
// return false, err
// } else if len(b) != docSize {
// return false, errors.New("invalid size")
// }
// return true, nil
// }
// }
//
// p := pool.NewLimited(20)
// defer p.Close()
// batch := p.Batch()
//
// go func() {
// for i := 0; i < 100; i++ {
// batch.Queue(test())
// }
// batch.QueueComplete()
// }()
//
// for res := range batch.Results() {
// Expect(res.Error()).ToNot(HaveOccurred())
// }
//
// })
})