forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_writer_test.go
137 lines (109 loc) · 3.19 KB
/
response_writer_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package porthos
import (
"encoding/json"
"os"
"testing"
"time"
"github.com/streadway/amqp"
)
type ResponseExample struct {
Sum float64 `json:"sum"`
}
func TestResponseWriterJSON(t *testing.T) {
b, _ := NewBroker(os.Getenv("AMQP_URL"))
ch, _ := b.openChannel()
q, _ := ch.QueueDeclare("", false, false, true, false, nil)
dc, _ := ch.Consume(q.Name, "", true, false, false, false, nil)
response := newResponse()
response.JSON(200, ResponseExample{Sum: 10})
rw := &responseWriter{
channel: ch,
autoAck: true,
delivery: amqp.Delivery{
ReplyTo: q.Name,
CorrelationId: "correlationId",
},
}
rw.Write(response)
select {
case response := <-dc:
if response.Headers["statusCode"].(int32) != 200 {
t.Errorf("Expected status code was 200, got: %d", response.Headers["statusCode"])
}
if response.ContentType != "application/json" {
t.Errorf("Expected content type application/json, got: %s", response.ContentType)
}
var responseTest ResponseExample
json.Unmarshal(response.Body, &responseTest)
if responseTest.Sum != 10 {
t.Errorf("Response failed, expected: 10, got: %s", responseTest.Sum)
}
return
case <-time.After(3 * time.Second):
t.Fatal("No response receive. Timedout.")
}
}
func TestResponseWriterRaw(t *testing.T) {
b, _ := NewBroker(os.Getenv("AMQP_URL"))
ch, _ := b.openChannel()
q, _ := ch.QueueDeclare("", false, false, true, false, nil)
dc, _ := ch.Consume(q.Name, "", true, false, false, false, nil)
response := newResponse()
response.Raw(201, "text/plain", []byte("Some Response Text"))
rw := &responseWriter{
channel: ch,
autoAck: true,
delivery: amqp.Delivery{
ReplyTo: q.Name,
CorrelationId: "correlationId",
},
}
rw.Write(response)
select {
case response := <-dc:
if response.Headers["statusCode"].(int32) != 201 {
t.Errorf("Expected status code was 201, got: %d", response.Headers["statusCode"])
}
if response.ContentType != "text/plain" {
t.Errorf("Expected content type text/plain, got: %s", response.ContentType)
}
if string(response.Body) != "Some Response Text" {
t.Errorf("Response failed, expected: Some Response Text, got: %s", string(response.Body))
}
return
case <-time.After(3 * time.Second):
t.Fatal("No response receive. Timedout.")
}
}
func TestResponseWriterEmpty(t *testing.T) {
b, _ := NewBroker(os.Getenv("AMQP_URL"))
ch, _ := b.openChannel()
q, _ := ch.QueueDeclare("", false, false, true, false, nil)
dc, _ := ch.Consume(q.Name, "", true, false, false, false, nil)
response := newResponse()
response.Empty(202)
rw := &responseWriter{
channel: ch,
autoAck: true,
delivery: amqp.Delivery{
ReplyTo: q.Name,
CorrelationId: "correlationId",
},
}
rw.Write(response)
select {
case response := <-dc:
if response.Headers["statusCode"].(int32) != 202 {
t.Errorf("Expected status code was 202, got: %d", response.Headers["statusCode"])
}
if response.ContentType != "" {
t.Errorf("Expected content type nil, got: %s", response.ContentType)
}
if string(response.Body) != "" {
t.Errorf("Response failed, expected: nil, got: %s", string(response.Body))
}
return
case <-time.After(3 * time.Second):
t.Fatal("No response receive. Timedout.")
}
}