log_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package middleware
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. const checkMark = "\u2713"
  20. const ballotX = "\u2717"
  21. func Test_ErrorLogger(t *testing.T) {
  22. middleware := ErrorLogger()
  23. assert.NotNil(t, middleware, "Can't get ErrorLogger middleware")
  24. }
  25. func Test_Logger(t *testing.T) {
  26. middleware := Logger()
  27. assert.NotNil(t, middleware, "Can't get Logger middleware")
  28. }
  29. func Test_colorForMethod(t *testing.T) {
  30. colors := map[string]string{
  31. "GET": blue,
  32. "POST": cyan,
  33. "PUT": yellow,
  34. "HEAD": magenta,
  35. "OPTIONS": white,
  36. "UNKNOWN": reset}
  37. for method, color := range colors {
  38. expect := colorForMethod(method)
  39. assert.NotNil(t, expect, "Can't get color from method %s %s",
  40. method, ballotX)
  41. t.Logf("Check if color %s%s%s is the right one for this method",
  42. string(expect), method, reset)
  43. if assert.Equal(t, expect, color, "Method %s has NOT the right color %s",
  44. method, ballotX) {
  45. t.Logf("Method %s has the right color %s", method, checkMark)
  46. }
  47. }
  48. }
  49. func Test_colorForStatus(t *testing.T) {
  50. colors := map[int]string{
  51. 200: green,
  52. 301: white,
  53. 404: yellow,
  54. 500: red}
  55. for status, color := range colors {
  56. expect := colorForStatus(status)
  57. assert.NotNil(t, expect, "Can't get color from status %d %s",
  58. status, ballotX)
  59. t.Logf("Check if color %s%d%s is the right one for this status",
  60. string(expect), status, reset)
  61. if assert.Equal(t, expect, color, "Status %d has NOT the right color %s",
  62. status, ballotX) {
  63. t.Logf("Status %d has the right color %s", status, checkMark)
  64. }
  65. }
  66. }