2
0

http_api_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // The MIT License (MIT)
  2. //
  3. // # Copyright (c) 2023 Winlin
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. // this software and associated documentation files (the "Software"), to deal in
  7. // the Software without restriction, including without limitation the rights to
  8. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. // the Software, and to permit persons to whom the Software is furnished to do so,
  10. // subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package blackbox
  22. import (
  23. "context"
  24. "fmt"
  25. "github.com/ossrs/go-oryx-lib/errors"
  26. "github.com/ossrs/go-oryx-lib/logger"
  27. "net/http"
  28. "sync"
  29. "testing"
  30. "time"
  31. )
  32. func TestFast_Http_Api_Basic_Auth(t *testing.T) {
  33. // This case is run in parallel.
  34. t.Parallel()
  35. // Setup the max timeout for this case.
  36. ctx, cancel := context.WithTimeout(logger.WithContext(context.Background()), time.Duration(*srsTimeout)*time.Millisecond)
  37. defer cancel()
  38. // Check a set of errors.
  39. var r0, r1, r2, r3, r4, r5, r6 error
  40. defer func(ctx context.Context) {
  41. if err := filterTestError(ctx.Err(), r0, r1, r2, r3, r4, r5, r6); err != nil {
  42. t.Errorf("Fail for err %+v", err)
  43. } else {
  44. logger.Tf(ctx, "test done with err %+v", err)
  45. }
  46. }(ctx)
  47. var wg sync.WaitGroup
  48. defer wg.Wait()
  49. // Start SRS server and wait for it to be ready.
  50. svr := NewSRSServer(func(v *srsServer) {
  51. v.envs = []string{
  52. "SRS_HTTP_API_AUTH_ENABLED=on",
  53. "SRS_HTTP_API_AUTH_USERNAME=admin",
  54. "SRS_HTTP_API_AUTH_PASSWORD=admin",
  55. }
  56. })
  57. wg.Add(1)
  58. go func() {
  59. defer wg.Done()
  60. r0 = svr.Run(ctx, cancel)
  61. }()
  62. <-svr.ReadyCtx().Done()
  63. if true {
  64. defer cancel()
  65. var res *http.Response
  66. url := fmt.Sprintf("http://admin:admin@localhost:%v/api/v1/versions", svr.APIPort())
  67. res, r1 = http.Get(url)
  68. if r1 == nil && res.StatusCode != 200 {
  69. r2 = errors.Errorf("get status code=%v, expect=200", res.StatusCode)
  70. }
  71. url = fmt.Sprintf("http://admin:123456@localhost:%v/api/v1/versions", svr.APIPort())
  72. res, r3 = http.Get(url)
  73. if r3 == nil && res.StatusCode != 401 {
  74. r4 = errors.Errorf("get status code=%v, expect=401", res.StatusCode)
  75. }
  76. url = fmt.Sprintf("http://localhost:%v/api/v1/versions", svr.APIPort())
  77. res, r5 = http.Get(url)
  78. if r5 == nil && res.StatusCode != 401 {
  79. r6 = errors.Errorf("get status code=%v, expect=401", res.StatusCode)
  80. }
  81. }
  82. }