add http server
This commit is contained in:
110
internal/api/http/server_test.go
Normal file
110
internal/api/http/server_test.go
Normal file
@ -0,0 +1,110 @@
|
||||
package http_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
stdhttp "net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.loyso.art/frx/devsim/internal/api/http"
|
||||
"git.loyso.art/frx/devsim/internal/entities"
|
||||
"git.loyso.art/frx/devsim/internal/store/mock"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func prepareEssentials(t testing.TB) (*mock.MockedStore, *slog.Logger) {
|
||||
t.Helper()
|
||||
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{}))
|
||||
return mock.NewMock(), log
|
||||
}
|
||||
|
||||
func TestList(t *testing.T) {
|
||||
require := require.New(t)
|
||||
store, log := prepareEssentials(t)
|
||||
|
||||
expectedStatistics := []entities.DeviceStatistics{
|
||||
{
|
||||
ID: entities.DeviceID("test-1"),
|
||||
OutgoingTrafficBytes: 10,
|
||||
},
|
||||
{
|
||||
ID: entities.DeviceID("test-2"),
|
||||
IncomingTrafficBytes: 20,
|
||||
},
|
||||
}
|
||||
|
||||
store.RegisterOnList(func() ([]entities.DeviceStatistics, error) {
|
||||
return expectedStatistics, nil
|
||||
})
|
||||
|
||||
hb := http.NewHandlersBuilder()
|
||||
hb.MountStatsHandlers(store, log)
|
||||
|
||||
handler := hb.Build()
|
||||
|
||||
server := httptest.NewServer(handler)
|
||||
defer server.Close()
|
||||
|
||||
httpClient := server.Client()
|
||||
req, err := stdhttp.NewRequest(stdhttp.MethodGet, server.URL+"/api/v1/stats", nil)
|
||||
require.NoError(err)
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
require.NoError(err)
|
||||
|
||||
stats := make([]entities.DeviceStatistics, 0, 2)
|
||||
err = json.NewDecoder(resp.Body).Decode(&stats)
|
||||
require.NoError(err)
|
||||
|
||||
require.ElementsMatch(stats, expectedStatistics)
|
||||
}
|
||||
|
||||
func TestUpsert(t *testing.T) {
|
||||
require := require.New(t)
|
||||
store, log := prepareEssentials(t)
|
||||
|
||||
expectedStatistics := entities.DeviceStatistics{
|
||||
ID: entities.DeviceID("test-1"),
|
||||
IncomingTrafficBytes: 5,
|
||||
OutgoingTrafficBytes: 10,
|
||||
IncomingRPS: 8,
|
||||
WriteRPS: 6,
|
||||
ReadRPS: 3,
|
||||
}
|
||||
|
||||
incomingStats := new(entities.DeviceStatistics)
|
||||
store.RegisterOnUpsert(func(ds entities.DeviceStatistics) error {
|
||||
*incomingStats = ds
|
||||
return nil
|
||||
})
|
||||
|
||||
hb := http.NewHandlersBuilder()
|
||||
hb.MountStatsHandlers(store, log)
|
||||
|
||||
handler := hb.Build()
|
||||
|
||||
server := httptest.NewServer(handler)
|
||||
defer server.Close()
|
||||
|
||||
httpClient := server.Client()
|
||||
|
||||
requestBody, _ := json.Marshal(map[string]any{
|
||||
"incoming_traffic": 5,
|
||||
"outgoing_traffic": 10,
|
||||
"incoming_rps": 8,
|
||||
"write_rps": 6,
|
||||
"read_rps": 3,
|
||||
})
|
||||
|
||||
req, err := stdhttp.NewRequest(stdhttp.MethodPost, server.URL+"/api/v1/stats/"+string(expectedStatistics.ID), bytes.NewReader(requestBody))
|
||||
require.NoError(err)
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
require.NoError(err)
|
||||
require.Equal(resp.StatusCode, stdhttp.StatusOK)
|
||||
require.Equal(*incomingStats, expectedStatistics)
|
||||
}
|
||||
Reference in New Issue
Block a user