Private
Public Access
1
0

Initial commit: conversaions, status, version

This commit is contained in:
2023-06-16 23:35:41 -07:00
commit a2b14c88ea
8 changed files with 361 additions and 0 deletions

56
web/server.go Normal file
View File

@@ -0,0 +1,56 @@
package web
import (
"encoding/json"
"fmt"
"log"
"net/http"
"code.severnaya.net/kordophone-mock/v2/server"
)
type MockHTTPServer struct {
Server server.Server
mux http.ServeMux
}
func (m *MockHTTPServer) handleVersion(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s", m.Server.Version())
}
func (m *MockHTTPServer) handleStatus(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "OK")
}
func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Request) {
convos := m.Server.Conversations()
// Encode convos as JSON
jsonData, err := json.Marshal(convos)
if err != nil {
log.Printf("Error marshalling conversations: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Write JSON to response
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
}
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.mux.ServeHTTP(w, r)
}
func NewMockHTTPServer() *MockHTTPServer {
this := MockHTTPServer{
Server: *server.NewServer(),
mux: *http.NewServeMux(),
}
this.mux.Handle("/version", http.HandlerFunc(this.handleVersion))
this.mux.Handle("/conversations", http.HandlerFunc(this.handleConversations))
this.mux.Handle("/status", http.HandlerFunc(this.handleStatus))
return &this
}

106
web/server_test.go Normal file
View File

@@ -0,0 +1,106 @@
package web_test
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"code.severnaya.net/kordophone-mock/v2/model"
"code.severnaya.net/kordophone-mock/v2/web"
)
func TestVersion(t *testing.T) {
s := httptest.NewServer(web.NewMockHTTPServer())
resp, err := http.Get(s.URL + "/version")
if err != nil {
t.Fatalf("TestVersion error: %s", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error decoding body: %s", body)
}
if string(body) != "1.0" {
t.Fatalf("Unexpected return value: %s (expected %s)", body, "1.0")
}
}
func TestStatus(t *testing.T) {
s := httptest.NewServer(web.NewMockHTTPServer())
resp, err := http.Get(s.URL + "/status")
if err != nil {
t.Fatalf("TestStatus error: %s", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error decoding body: %s", body)
}
if string(body) != "OK" {
t.Fatalf("Unexpected return value: %s (expected %s)", body, "OK")
}
}
func TestConversations(t *testing.T) {
server := web.NewMockHTTPServer()
httpServer := httptest.NewServer(server)
conversation := model.Conversation{
Date: time.Now(),
Participants: []string{"Alice", "Bob"},
UnreadCount: 1,
LastMessagePreview: "Hello world",
Guid: "1234567890",
}
server.Server.AddConversation(conversation)
resp, err := http.Get(httpServer.URL + "/conversations")
if err != nil {
t.Fatalf("TestConversations error: %s", err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error decoding body: %s", body)
}
var convos []model.Conversation
err = json.Unmarshal(body, &convos)
if err != nil {
t.Fatalf("Error unmarshalling JSON: %s", err)
}
if len(convos) != 1 {
t.Fatalf("Unexpected number of conversations: %d (expected %d)", len(convos), 1)
}
if convos[0].Guid != conversation.Guid {
t.Fatalf("Unexpected conversation GUID: %s (expected %s)", convos[0].Guid, conversation.Guid)
}
if convos[0].LastMessagePreview != conversation.LastMessagePreview {
t.Fatalf("Unexpected conversation LastMessagePreview: %s (expected %s)", convos[0].LastMessagePreview, conversation.LastMessagePreview)
}
if convos[0].UnreadCount != conversation.UnreadCount {
t.Fatalf("Unexpected conversation UnreadCount: %d (expected %d)", convos[0].UnreadCount, conversation.UnreadCount)
}
for i, p := range convos[0].Participants {
if p != conversation.Participants[i] {
t.Fatalf("Unexpected conversation Participants: %s (expected %s)", p, conversation.Participants[i])
}
}
if !convos[0].Date.Equal(conversation.Date) {
t.Fatalf("Unexpected conversation Date: %s (expected %s)", convos[0].Date, conversation.Date)
}
}