z_test.go 1.4 KB

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