-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.go
164 lines (149 loc) · 4.77 KB
/
utils_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2022 SundaeSwap Labs, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// Licensed under the MIT License;
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package kugo
import (
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"strings"
)
type DatumResponse struct {
Datum string
}
type MockKugoServer struct {
datums map[string]DatumResponse
matches map[string][]Match
metadata map[int]map[string][]Metadatum
patterns []string
scripts map[string]Script
}
func NewMockServer() *MockKugoServer {
return &MockKugoServer{}
}
func (m *MockKugoServer) AddScripts(script ...Script) *MockKugoServer {
if m.scripts == nil {
m.scripts = make(map[string]Script)
}
for _, script := range script {
m.scripts[hex.EncodeToString(script.Hash())] = script
}
return m
}
func (m *MockKugoServer) AddPatterns(patterns ...string) *MockKugoServer {
m.patterns = patterns
return m
}
func (m *MockKugoServer) AddMatches(pattern string, matches ...Match) *MockKugoServer {
if m.matches == nil {
m.matches = make(map[string][]Match)
}
m.matches[pattern] = append(m.matches[pattern], matches...)
return m
}
func (m *MockKugoServer) AddDatum(hash string, datum string) *MockKugoServer {
if m.datums == nil {
m.datums = make(map[string]DatumResponse)
}
m.datums[hash] = DatumResponse{Datum: datum}
return m
}
type MetadatumEntry struct {
Slot int
Tx string
Metadatum Metadatum
}
func (m *MockKugoServer) AddMetadata(entries ...MetadatumEntry) *MockKugoServer {
if m.metadata == nil {
m.metadata = make(map[int]map[string][]Metadatum)
}
for _, entry := range entries {
if _, ok := m.metadata[entry.Slot]; !ok {
m.metadata[entry.Slot] = make(map[string][]Metadatum)
}
m.metadata[entry.Slot][entry.Tx] = append(m.metadata[entry.Slot][entry.Tx], entry.Metadatum)
}
return m
}
type ErrorResponse struct {
Hint string `json:"hint"`
}
func writeError(w http.ResponseWriter, code int, hint string) {
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(ErrorResponse{Hint: hint})
}
func writeSuccess(w http.ResponseWriter, body interface{}) {
respBody, _ := json.Marshal(body)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(respBody)
}
func (m *MockKugoServer) HTTP() *httptest.Server {
return httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/v1/script/") {
response, ok := m.scripts[strings.TrimPrefix(r.URL.Path, "/v1/script/")]
if !ok {
// If a script isn't found, Kugo responds with `null` instead of an error.
writeSuccess(w, json.RawMessage("null"))
} else {
writeSuccess(w, &response)
}
} else if r.URL.Path == "/v1/patterns" {
writeSuccess(w, m.patterns)
} else if strings.HasPrefix(r.URL.Path, "/v1/metadata/") {
slotStr := strings.TrimPrefix(r.URL.Path, "/v1/metadata/")
slot, _ := strconv.Atoi(slotStr)
tx := r.URL.Query().Get("transaction_id")
var metadata []Metadatum
if tx == "" {
for _, m := range m.metadata[slot] {
metadata = append(metadata, m...)
}
} else {
metadata = append(metadata, m.metadata[slot][tx]...)
}
writeSuccess(w, &metadata)
} else if strings.HasPrefix(r.URL.Path, "/v1/matches") {
pattern := strings.TrimPrefix(r.URL.Path, "/v1/matches/")
// TODO: filter by other query parameters
matches, ok := m.matches[pattern]
if !ok {
writeError(w, http.StatusNotFound, "pattern not found")
} else {
writeSuccess(w, &matches)
}
} else if strings.HasPrefix(r.URL.Path, "/v1/datums/") {
hash := strings.TrimPrefix(r.URL.Path, "/v1/datums/")
datum, ok := m.datums[hash]
if !ok {
writeError(w, http.StatusNotFound, "datum not found")
} else {
writeSuccess(w, &datum)
}
} else {
w.WriteHeader(http.StatusNotFound)
}
}),
)
}