This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chain_test.go
114 lines (100 loc) · 2.4 KB
/
chain_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
// Goro
//
// Created by Yakka
// http://theyakka.com
//
// Copyright (c) 2019 Yakka LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package goro_test
import (
"errors"
"github.com/theyakka/goro"
"testing"
)
func TestSimpleChain(t *testing.T) {
path := "/chain/simple"
Debug("Requesting", path, "...")
execMockRequest(router, "GET", path)
expectedSum := 13
if sum != expectedSum {
t.Error("Expected a sum of", expectedSum, "but got", sum)
}
resetState()
}
func TestMultiCallChain(t *testing.T) {
path := "/chain/simple"
Debug("Requesting", path, "...")
execMockRequest(router, "GET", path)
expectedSum := 13
if sum != expectedSum {
t.Error("Expected a sum of", expectedSum, "but got", sum)
}
sum = 0
execMockRequest(router, "GET", path)
if sum != expectedSum {
t.Error("Expected a sum of", expectedSum, "but got", sum)
}
resetState()
}
func TestThenChain(t *testing.T) {
path := "/chain/then"
Debug("Requesting", path, "...")
execMockRequest(router, "GET", path)
expectedSum := 23
if sum != expectedSum {
t.Error("Expected a sum of", expectedSum, "but got", sum)
}
resetState()
}
func TestHaltChain(t *testing.T) {
path := "/chain/halt"
Debug("Requesting", path, "...")
execMockRequest(router, "GET", path)
expectedSum := 5
if sum != expectedSum {
t.Error("Expected a sum of", expectedSum, "but got", sum)
}
resetState()
}
func TestErrorChain(t *testing.T) {
path := "/chain/error"
Debug("Requesting", path, "...")
execMockRequest(router, "GET", path)
expectedSum := 777
if sum != expectedSum {
t.Error("Expected a sum of", expectedSum, "but got", sum)
}
resetState()
}
func testThenHandler(_ *goro.HandlerContext) {
Debug("Chain hit then")
sum += 10
}
func chainHandler1(ch *goro.Chain, ctx *goro.HandlerContext) {
Debug("Chain hit 1")
sum = 1
ch.Next(ctx)
}
func chainHandler2(ch *goro.Chain, ctx *goro.HandlerContext) {
Debug("Chain hit 2")
sum += 4
ch.Next(ctx)
}
func chainHandler3(ch *goro.Chain, ctx *goro.HandlerContext) {
Debug("Chain hit 3")
sum += 8
ch.Next(ctx)
}
func testHaltHandler(ch *goro.Chain, ctx *goro.HandlerContext) {
Debug("Chain hit halt")
ch.Halt(ctx)
}
func testErrorChainHandler(ch *goro.Chain, ctx *goro.HandlerContext) {
Debug("Chain error hit")
ch.Error(ctx, errors.New("the chain hit an error"), 777)
}
func chainCustomErrorHandler(ctx *goro.HandlerContext) {
Debug("Error handler hit")
sum = 777
}