Compare commits
No commits in common. "f6979868e5c45703f766410fa5f43b9e0bb3e4e2" and "10de039da031781588f3f0b106abe81139412e68" have entirely different histories.
f6979868e5
...
10de039da0
@ -11,7 +11,7 @@ import (
|
|||||||
"git.defalsify.org/vise.git/db"
|
"git.defalsify.org/vise.git/db"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.defalsify.org/vise.git/state"
|
"git.defalsify.org/vise.git/state"
|
||||||
"git.grassecon.net/urdt/ussd/internal/mocks"
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd/mocks"
|
||||||
"git.grassecon.net/urdt/ussd/internal/models"
|
"git.grassecon.net/urdt/ussd/internal/models"
|
||||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||||
"github.com/alecthomas/assert/v2"
|
"github.com/alecthomas/assert/v2"
|
||||||
|
@ -1,232 +0,0 @@
|
|||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/engine"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/mocks/httpmocks"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNewATSessionHandler(t *testing.T) {
|
|
||||||
mockHandler := &httpmocks.MockRequestHandler{}
|
|
||||||
ash := NewATSessionHandler(mockHandler)
|
|
||||||
|
|
||||||
if ash == nil {
|
|
||||||
t.Fatal("NewATSessionHandler returned nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
if ash.SessionHandler == nil {
|
|
||||||
t.Fatal("SessionHandler is nil")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestATSessionHandler_ServeHTTP(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
setupMocks func(*httpmocks.MockRequestHandler, *httpmocks.MockRequestParser, *httpmocks.MockEngine)
|
|
||||||
formData url.Values
|
|
||||||
expectedStatus int
|
|
||||||
expectedBody string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Successful request",
|
|
||||||
setupMocks: func(mh *httpmocks.MockRequestHandler, mrp *httpmocks.MockRequestParser, me *httpmocks.MockEngine) {
|
|
||||||
mrp.GetSessionIdFunc = func(rq any) (string, error) {
|
|
||||||
req := rq.(*http.Request)
|
|
||||||
return req.FormValue("phoneNumber"), nil
|
|
||||||
}
|
|
||||||
mrp.GetInputFunc = func(rq any) ([]byte, error) {
|
|
||||||
req := rq.(*http.Request)
|
|
||||||
text := req.FormValue("text")
|
|
||||||
parts := strings.Split(text, "*")
|
|
||||||
return []byte(parts[len(parts)-1]), nil
|
|
||||||
}
|
|
||||||
mh.ProcessFunc = func(rqs handlers.RequestSession) (handlers.RequestSession, error) {
|
|
||||||
rqs.Continue = true
|
|
||||||
rqs.Engine = me
|
|
||||||
return rqs, nil
|
|
||||||
}
|
|
||||||
mh.GetConfigFunc = func() engine.Config { return engine.Config{} }
|
|
||||||
mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp }
|
|
||||||
mh.OutputFunc = func(rs handlers.RequestSession) (handlers.RequestSession, error) { return rs, nil }
|
|
||||||
mh.ResetFunc = func(rs handlers.RequestSession) (handlers.RequestSession, error) { return rs, nil }
|
|
||||||
me.WriteResultFunc = func(context.Context, io.Writer) (int, error) { return 0, nil }
|
|
||||||
},
|
|
||||||
formData: url.Values{
|
|
||||||
"phoneNumber": []string{"+1234567890"},
|
|
||||||
"text": []string{"1*2*3"},
|
|
||||||
},
|
|
||||||
expectedStatus: http.StatusOK,
|
|
||||||
expectedBody: "CON ",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "GetSessionId error",
|
|
||||||
setupMocks: func(mh *httpmocks.MockRequestHandler, mrp *httpmocks.MockRequestParser, me *httpmocks.MockEngine) {
|
|
||||||
mrp.GetSessionIdFunc = func(rq any) (string, error) {
|
|
||||||
return "", errors.New("no phone number found")
|
|
||||||
}
|
|
||||||
mh.GetConfigFunc = func() engine.Config { return engine.Config{} }
|
|
||||||
mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp }
|
|
||||||
},
|
|
||||||
formData: url.Values{
|
|
||||||
"text": []string{"1*2*3"},
|
|
||||||
},
|
|
||||||
expectedStatus: http.StatusBadRequest,
|
|
||||||
expectedBody: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "GetInput error",
|
|
||||||
setupMocks: func(mh *httpmocks.MockRequestHandler, mrp *httpmocks.MockRequestParser, me *httpmocks.MockEngine) {
|
|
||||||
mrp.GetSessionIdFunc = func(rq any) (string, error) {
|
|
||||||
req := rq.(*http.Request)
|
|
||||||
return req.FormValue("phoneNumber"), nil
|
|
||||||
}
|
|
||||||
mrp.GetInputFunc = func(rq any) ([]byte, error) {
|
|
||||||
return nil, errors.New("no input found")
|
|
||||||
}
|
|
||||||
mh.GetConfigFunc = func() engine.Config { return engine.Config{} }
|
|
||||||
mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp }
|
|
||||||
},
|
|
||||||
formData: url.Values{
|
|
||||||
"phoneNumber": []string{"+1234567890"},
|
|
||||||
},
|
|
||||||
expectedStatus: http.StatusBadRequest,
|
|
||||||
expectedBody: "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Process error",
|
|
||||||
setupMocks: func(mh *httpmocks.MockRequestHandler, mrp *httpmocks.MockRequestParser, me *httpmocks.MockEngine) {
|
|
||||||
mrp.GetSessionIdFunc = func(rq any) (string, error) {
|
|
||||||
req := rq.(*http.Request)
|
|
||||||
return req.FormValue("phoneNumber"), nil
|
|
||||||
}
|
|
||||||
mrp.GetInputFunc = func(rq any) ([]byte, error) {
|
|
||||||
req := rq.(*http.Request)
|
|
||||||
text := req.FormValue("text")
|
|
||||||
parts := strings.Split(text, "*")
|
|
||||||
return []byte(parts[len(parts)-1]), nil
|
|
||||||
}
|
|
||||||
mh.ProcessFunc = func(rqs handlers.RequestSession) (handlers.RequestSession, error) {
|
|
||||||
return rqs, handlers.ErrStorage
|
|
||||||
}
|
|
||||||
mh.GetConfigFunc = func() engine.Config { return engine.Config{} }
|
|
||||||
mh.GetRequestParserFunc = func() handlers.RequestParser { return mrp }
|
|
||||||
},
|
|
||||||
formData: url.Values{
|
|
||||||
"phoneNumber": []string{"+1234567890"},
|
|
||||||
"text": []string{"1*2*3"},
|
|
||||||
},
|
|
||||||
expectedStatus: http.StatusInternalServerError,
|
|
||||||
expectedBody: "",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
mockHandler := &httpmocks.MockRequestHandler{}
|
|
||||||
mockRequestParser := &httpmocks.MockRequestParser{}
|
|
||||||
mockEngine := &httpmocks.MockEngine{}
|
|
||||||
tt.setupMocks(mockHandler, mockRequestParser, mockEngine)
|
|
||||||
|
|
||||||
ash := NewATSessionHandler(mockHandler)
|
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tt.formData.Encode()))
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
|
|
||||||
ash.ServeHTTP(w, req)
|
|
||||||
|
|
||||||
if w.Code != tt.expectedStatus {
|
|
||||||
t.Errorf("Expected status %d, got %d", tt.expectedStatus, w.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tt.expectedBody != "" && w.Body.String() != tt.expectedBody {
|
|
||||||
t.Errorf("Expected body %q, got %q", tt.expectedBody, w.Body.String())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestATSessionHandler_Output(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
input handlers.RequestSession
|
|
||||||
expectedPrefix string
|
|
||||||
expectedError bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Continue true",
|
|
||||||
input: handlers.RequestSession{
|
|
||||||
Continue: true,
|
|
||||||
Engine: &httpmocks.MockEngine{
|
|
||||||
WriteResultFunc: func(context.Context, io.Writer) (int, error) {
|
|
||||||
return 0, nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Writer: &httpmocks.MockWriter{},
|
|
||||||
},
|
|
||||||
expectedPrefix: "CON ",
|
|
||||||
expectedError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Continue false",
|
|
||||||
input: handlers.RequestSession{
|
|
||||||
Continue: false,
|
|
||||||
Engine: &httpmocks.MockEngine{
|
|
||||||
WriteResultFunc: func(context.Context, io.Writer) (int, error) {
|
|
||||||
return 0, nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Writer: &httpmocks.MockWriter{},
|
|
||||||
},
|
|
||||||
expectedPrefix: "END ",
|
|
||||||
expectedError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "WriteResult error",
|
|
||||||
input: handlers.RequestSession{
|
|
||||||
Continue: true,
|
|
||||||
Engine: &httpmocks.MockEngine{
|
|
||||||
WriteResultFunc: func(context.Context, io.Writer) (int, error) {
|
|
||||||
return 0, errors.New("write error")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Writer: &httpmocks.MockWriter{},
|
|
||||||
},
|
|
||||||
expectedPrefix: "CON ",
|
|
||||||
expectedError: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
ash := &ATSessionHandler{}
|
|
||||||
_, err := ash.Output(tt.input)
|
|
||||||
|
|
||||||
if tt.expectedError && err == nil {
|
|
||||||
t.Error("Expected an error, but got nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !tt.expectedError && err != nil {
|
|
||||||
t.Errorf("Unexpected error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mw := tt.input.Writer.(*httpmocks.MockWriter)
|
|
||||||
if !mw.WriteStringCalled {
|
|
||||||
t.Error("WriteString was not called")
|
|
||||||
}
|
|
||||||
|
|
||||||
if mw.WrittenString != tt.expectedPrefix {
|
|
||||||
t.Errorf("Expected prefix %q, got %q", tt.expectedPrefix, mw.WrittenString)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,30 +0,0 @@
|
|||||||
package httpmocks
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MockEngine implements the engine.Engine interface for testing
|
|
||||||
type MockEngine struct {
|
|
||||||
InitFunc func(context.Context) (bool, error)
|
|
||||||
ExecFunc func(context.Context, []byte) (bool, error)
|
|
||||||
WriteResultFunc func(context.Context, io.Writer) (int, error)
|
|
||||||
FinishFunc func() error
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockEngine) Init(ctx context.Context) (bool, error) {
|
|
||||||
return m.InitFunc(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockEngine) Exec(ctx context.Context, input []byte) (bool, error) {
|
|
||||||
return m.ExecFunc(ctx, input)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockEngine) WriteResult(ctx context.Context, w io.Writer) (int, error) {
|
|
||||||
return m.WriteResultFunc(ctx, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockEngine) Finish() error {
|
|
||||||
return m.FinishFunc()
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
package httpmocks
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.defalsify.org/vise.git/engine"
|
|
||||||
"git.defalsify.org/vise.git/persist"
|
|
||||||
"git.defalsify.org/vise.git/resource"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MockRequestHandler implements handlers.RequestHandler interface for testing
|
|
||||||
type MockRequestHandler struct {
|
|
||||||
ProcessFunc func(handlers.RequestSession) (handlers.RequestSession, error)
|
|
||||||
GetConfigFunc func() engine.Config
|
|
||||||
GetEngineFunc func(cfg engine.Config, rs resource.Resource, pe *persist.Persister) engine.Engine
|
|
||||||
OutputFunc func(rs handlers.RequestSession) (handlers.RequestSession, error)
|
|
||||||
ResetFunc func(rs handlers.RequestSession) (handlers.RequestSession, error)
|
|
||||||
ShutdownFunc func()
|
|
||||||
GetRequestParserFunc func() handlers.RequestParser
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestHandler) Process(rqs handlers.RequestSession) (handlers.RequestSession, error) {
|
|
||||||
return m.ProcessFunc(rqs)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestHandler) GetConfig() engine.Config {
|
|
||||||
return m.GetConfigFunc()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestHandler) GetEngine(cfg engine.Config, rs resource.Resource, pe *persist.Persister) engine.Engine {
|
|
||||||
return m.GetEngineFunc(cfg, rs, pe)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestHandler) Output(rs handlers.RequestSession) (handlers.RequestSession, error) {
|
|
||||||
return m.OutputFunc(rs)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestHandler) Reset(rs handlers.RequestSession) (handlers.RequestSession, error) {
|
|
||||||
return m.ResetFunc(rs)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestHandler) Shutdown() {
|
|
||||||
m.ShutdownFunc()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestHandler) GetRequestParser() handlers.RequestParser {
|
|
||||||
return m.GetRequestParserFunc()
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package httpmocks
|
|
||||||
|
|
||||||
// MockRequestParser implements the handlers.RequestParser interface for testing
|
|
||||||
type MockRequestParser struct {
|
|
||||||
GetSessionIdFunc func(any) (string, error)
|
|
||||||
GetInputFunc func(any) ([]byte, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestParser) GetSessionId(rq any) (string, error) {
|
|
||||||
return m.GetSessionIdFunc(rq)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockRequestParser) GetInput(rq any) ([]byte, error) {
|
|
||||||
return m.GetInputFunc(rq)
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package httpmocks
|
|
||||||
|
|
||||||
import "net/http"
|
|
||||||
|
|
||||||
// MockWriter implements a mock io.Writer for testing
|
|
||||||
type MockWriter struct {
|
|
||||||
WriteStringCalled bool
|
|
||||||
WrittenString string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockWriter) Write(p []byte) (n int, err error) {
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockWriter) WriteString(s string) (n int, err error) {
|
|
||||||
m.WriteStringCalled = true
|
|
||||||
m.WrittenString = s
|
|
||||||
return len(s), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockWriter) Header() http.Header {
|
|
||||||
return http.Header{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *MockWriter) WriteHeader(statusCode int) {}
|
|
Loading…
Reference in New Issue
Block a user