z_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package application_test
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "time"
  7. "git.ali33.ru/fcg-xvii/rest"
  8. "git.ali33.ru/fcg-xvii/rest/application"
  9. )
  10. type Hello struct {
  11. Name string `rest:"required"`
  12. }
  13. func (s *Hello) Validate(req rest.IRequestIn) rest.IRequestOut {
  14. if s.Name = strings.TrimSpace(s.Name); len(s.Name) == 0 {
  15. return req.OutError(rest.ErrorFiled("name", "expected not empty string"))
  16. }
  17. return nil
  18. }
  19. func (s *Hello) Execute(req rest.IRequestIn) rest.IRequestOut {
  20. response := HelloResponse{
  21. Message: "Hello, " + s.Name,
  22. }
  23. return rest.OutFileds(req, &response, nil)
  24. }
  25. type HelloResponse struct {
  26. Message string `rest:"default"`
  27. }
  28. ////////////////////////////
  29. var (
  30. commands = map[string]func() rest.IExecuter{
  31. "/api/hello": func() rest.IExecuter { return &Hello{} },
  32. }
  33. )
  34. func Command(cName string) (rest.IExecuter, bool) {
  35. if command, check := commands[cName]; check {
  36. return command(), true
  37. }
  38. return nil, false
  39. }
  40. func TestApp(t *testing.T) {
  41. conf := &application.AppConfig{
  42. Addr: "192.168.100.3:5000",
  43. Http: true,
  44. Websocket: false,
  45. Secret: []byte("secret"),
  46. HttpPrefix: "/api/",
  47. WebsocketPrefix: "/ws/",
  48. HTTPHeaders: application.HTTPHeadersCrossOrigin(),
  49. Commands: Command,
  50. }
  51. app := application.New(conf, nil, context.Background())
  52. t.Log(app)
  53. <-time.After(time.Hour)
  54. }