Skip to content

Commit 1a8fc54

Browse files
authored
Merge pull request #718 from marle3003/develop
Develop
2 parents ab3a581 + 4919e26 commit 1a8fc54

File tree

29 files changed

+724
-147
lines changed

29 files changed

+724
-147
lines changed

api/handler_events_test.go

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package api
22

33
import (
4+
"context"
5+
"encoding/json"
46
"fmt"
5-
"github.com/stretchr/testify/require"
67
"mokapi/config/static"
8+
"mokapi/providers/openapi"
79
"mokapi/runtime"
810
"mokapi/runtime/events"
911
"mokapi/runtime/events/eventstest"
1012
"mokapi/try"
1113
"net/http"
14+
"net/http/httptest"
1215
"testing"
1316
"time"
17+
18+
"github.com/stretchr/testify/require"
1419
)
1520

1621
func TestHandler_Events(t *testing.T) {
@@ -73,7 +78,109 @@ func TestHandler_Events(t *testing.T) {
7378
},
7479
},
7580
{
76-
name: "get specific event but not existing",
81+
name: "get http event with header parameter as string",
82+
fn: func(t *testing.T, h http.Handler, sm *events.StoreManager) {
83+
sm.SetStore(1, events.NewTraits().WithNamespace("http"))
84+
85+
r := httptest.NewRequest("get", "http://localhost/foo", nil)
86+
r.Header.Set("foo", "bar")
87+
88+
params := &openapi.RequestParameters{Header: map[string]openapi.RequestParameterValue{}}
89+
v := "bar"
90+
params.Header["Foo"] = openapi.RequestParameterValue{
91+
Value: "bar",
92+
Raw: &v,
93+
}
94+
r = r.WithContext(openapi.NewContext(context.Background(), params))
95+
96+
_, err := openapi.NewLogEventContext(r, false, sm, events.NewTraits().WithNamespace("http"))
97+
require.NoError(t, err)
98+
try.Handler(t,
99+
http.MethodGet,
100+
"http://foo.api/api/events?namespace=http",
101+
nil,
102+
"",
103+
h,
104+
try.HasStatusCode(200),
105+
try.AssertBody(func(t *testing.T, body string) {
106+
var m []map[string]any
107+
require.NoError(t, json.Unmarshal([]byte(body), &m))
108+
require.Equal(t, map[string]interface{}{
109+
"actions": interface{}(nil),
110+
"api": "",
111+
"deprecated": false,
112+
"duration": float64(0),
113+
"path": "",
114+
"request": map[string]interface{}{
115+
"method": "get",
116+
"parameters": []interface{}{
117+
map[string]interface{}{
118+
"name": "Foo",
119+
"raw": "bar",
120+
"type": "header",
121+
"value": "\"bar\"",
122+
},
123+
},
124+
"url": "http://localhost/foo",
125+
},
126+
"response": map[string]interface{}{"body": "", "size": float64(0), "statusCode": float64(0)}},
127+
m[0]["data"])
128+
}))
129+
},
130+
},
131+
{
132+
name: "get http event with header parameter as object",
133+
fn: func(t *testing.T, h http.Handler, sm *events.StoreManager) {
134+
sm.SetStore(1, events.NewTraits().WithNamespace("http"))
135+
136+
r := httptest.NewRequest("get", "http://localhost/foo", nil)
137+
r.Header.Set("foo", "role,admin,firstName,Alex")
138+
139+
params := &openapi.RequestParameters{Header: map[string]openapi.RequestParameterValue{}}
140+
v := "bar"
141+
params.Header["Foo"] = openapi.RequestParameterValue{
142+
Value: map[string]any{"role": "admin", "firstName": "Alex"},
143+
Raw: &v,
144+
}
145+
r = r.WithContext(openapi.NewContext(context.Background(), params))
146+
147+
_, err := openapi.NewLogEventContext(r, false, sm, events.NewTraits().WithNamespace("http"))
148+
require.NoError(t, err)
149+
try.Handler(t,
150+
http.MethodGet,
151+
"http://foo.api/api/events?namespace=http",
152+
nil,
153+
"",
154+
h,
155+
try.HasStatusCode(200),
156+
try.AssertBody(func(t *testing.T, body string) {
157+
var m []map[string]any
158+
require.NoError(t, json.Unmarshal([]byte(body), &m))
159+
require.Equal(t, map[string]interface{}{
160+
"actions": interface{}(nil),
161+
"api": "",
162+
"deprecated": false,
163+
"duration": float64(0),
164+
"path": "",
165+
"request": map[string]interface{}{
166+
"method": "get",
167+
"parameters": []interface{}{
168+
map[string]interface{}{
169+
"name": "Foo",
170+
"raw": "bar",
171+
"type": "header",
172+
"value": "{\"firstName\":\"Alex\",\"role\":\"admin\"}",
173+
},
174+
},
175+
"url": "http://localhost/foo",
176+
},
177+
"response": map[string]interface{}{"body": "", "size": float64(0), "statusCode": float64(0)}},
178+
m[0]["data"])
179+
}))
180+
},
181+
},
182+
{
183+
name: "http with request parameter",
77184
fn: func(t *testing.T, h http.Handler, sm *events.StoreManager) {
78185
try.Handler(t,
79186
http.MethodGet,

engine/common/host.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,10 @@ type FakerNode interface {
174174
type Store interface {
175175
Get(string) any
176176
Set(string, any)
177+
Has(string) bool
178+
Delete(string)
179+
Clear()
180+
Update(key string, fn func(v any) any) any
181+
Keys() []string
182+
Namespace(name string) Store
177183
}

engine/engine.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"mokapi/runtime"
88
"mokapi/runtime/events"
99
"mokapi/runtime/metrics"
10+
"sort"
1011
"sync"
1112

1213
"github.com/pkg/errors"
@@ -163,6 +164,10 @@ func (e *Engine) IsLevelEnabled(level string) bool {
163164
return e.logger.IsLevelEnabled(level)
164165
}
165166

167+
func NewStore() *Store {
168+
return &Store{data: make(map[string]any)}
169+
}
170+
166171
func (s *Store) Get(key string) any {
167172
s.mu.RLock()
168173
defer s.mu.RUnlock()
@@ -174,3 +179,61 @@ func (s *Store) Set(key string, value any) {
174179
defer s.mu.Unlock()
175180
s.data[key] = value
176181
}
182+
183+
func (s *Store) Has(key string) bool {
184+
s.mu.RLock()
185+
defer s.mu.RUnlock()
186+
_, ok := s.data[key]
187+
return ok
188+
}
189+
190+
func (s *Store) Delete(key string) {
191+
s.mu.Lock()
192+
defer s.mu.Unlock()
193+
delete(s.data, key)
194+
}
195+
196+
func (s *Store) Clear() {
197+
s.mu.Lock()
198+
defer s.mu.Unlock()
199+
s.data = make(map[string]any)
200+
}
201+
202+
func (s *Store) Keys() []string {
203+
s.mu.RLock()
204+
defer s.mu.RUnlock()
205+
keys := make([]string, 0, len(s.data))
206+
for k := range s.data {
207+
keys = append(keys, k)
208+
}
209+
sort.Strings(keys)
210+
return keys
211+
}
212+
213+
func (s *Store) Update(key string, fn func(v any) any) any {
214+
s.mu.Lock()
215+
defer s.mu.Unlock()
216+
v := fn(s.data[key])
217+
s.data[key] = v
218+
return v
219+
}
220+
221+
func (s *Store) Namespace(name string) common.Store {
222+
s.mu.Lock()
223+
v, ok := s.data[name]
224+
s.mu.Unlock()
225+
if ok {
226+
ns, ok := v.(*Store)
227+
228+
if !ok {
229+
return nil
230+
}
231+
return ns
232+
}
233+
234+
s.mu.Lock()
235+
defer s.mu.Unlock()
236+
ns := &Store{data: make(map[string]any)}
237+
s.data[name] = ns
238+
return ns
239+
}

engine/enginetest/host.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package enginetest
33
import (
44
"fmt"
55
"mokapi/config/dynamic"
6+
"mokapi/engine"
67
"mokapi/engine/common"
78
"mokapi/schema/json/generator"
89
"net/http"
@@ -27,12 +28,7 @@ type Host struct {
2728
OnFunc func(event string, do func(args ...interface{}) (bool, error), tags map[string]string)
2829
FindFakerNodeFunc func(name string) *generator.Node
2930
m sync.Mutex
30-
StoreTest *Store
31-
}
32-
33-
type Store struct {
34-
data map[string]any
35-
m sync.RWMutex
31+
StoreTest *engine.Store
3632
}
3733

3834
type HttpClient struct {
@@ -142,7 +138,7 @@ func (h *Host) KafkaClient() common.KafkaClient {
142138

143139
func (h *Host) Store() common.Store {
144140
if h.StoreTest == nil {
145-
h.StoreTest = &Store{data: make(map[string]any)}
141+
h.StoreTest = engine.NewStore()
146142
}
147143
return h.StoreTest
148144
}
@@ -173,21 +169,3 @@ func mustParse(s string) *url.URL {
173169
}
174170
return u
175171
}
176-
177-
func (s *Store) Get(name string) any {
178-
s.m.RLock()
179-
defer s.m.RUnlock()
180-
if s.data == nil {
181-
return nil
182-
}
183-
return s.data[name]
184-
}
185-
186-
func (s *Store) Set(name string, value any) {
187-
s.m.Lock()
188-
defer s.m.Unlock()
189-
if s.data == nil {
190-
s.data = map[string]any{name: value}
191-
}
192-
s.data[name] = value
193-
}

examples/mokapi/services_http.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,5 +775,50 @@ export let events = [
775775
}
776776
]
777777
}
778+
},
779+
{
780+
id: "a5409fc1-6a5a-4c0b-be39-a6b80885e41b",
781+
traits: {
782+
namespace: "http",
783+
name: "Swagger Petstore",
784+
path: "/requestWithParams",
785+
method: 'GET'
786+
},
787+
time: '2023-02-13T10:05:25.583366+01:00',
788+
data: {
789+
request: {
790+
method: "GET",
791+
url: "http://127.0.0.1:18080/requestWithParams",
792+
contentType: "application/json",
793+
parameters: [
794+
{
795+
name: "foo",
796+
type: "query",
797+
value: `"bar"`,
798+
raw: "bar"
799+
},
800+
{
801+
name: "debug",
802+
type: "header",
803+
value: `{"foo":"bar"}`,
804+
raw: "bar"
805+
},
806+
{
807+
name: "noSpec",
808+
type: "header",
809+
raw: "bar"
810+
}
811+
]
812+
},
813+
response: {
814+
statusCode: 500,
815+
headers: {
816+
"Content-Type": "application/json",
817+
},
818+
body: `{"id": 0,"category": {"id": 0,"name": "string"},"name": "doggie","photoUrls": ["string"],"tags":[{"id": 0,"name": "string"}],"status": "available"}`,
819+
size: 512
820+
},
821+
duration: 133,
822+
}
778823
}
779824
]

imap/auth.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package imap
22

33
import (
44
"encoding/base64"
5+
"fmt"
56
"mokapi/sasl"
67
"strings"
78
)
@@ -74,9 +75,11 @@ func (c *conn) handleAuth(_, param string) *response {
7475

7576
c.state = AuthenticatedState
7677

78+
caps := c.getCapabilities()
7779
return &response{
7880
status: ok,
79-
text: "Authenticated",
81+
code: responseCode(fmt.Sprintf("%s", caps)),
82+
text: "Logged in",
8083
}
8184
}
8285

@@ -95,9 +98,13 @@ func (c *conn) handleLogin(tag, param string) error {
9598
if err != nil {
9699
return err
97100
}
101+
102+
c.state = AuthenticatedState
103+
104+
caps := c.getCapabilities()
98105
return c.writeResponse(tag, &response{
99106
status: ok,
100-
code: readWrite,
101-
text: "SELECT completed",
107+
code: responseCode(fmt.Sprintf("%s", caps)),
108+
text: "Logged in",
102109
})
103110
}

0 commit comments

Comments
 (0)