go test -run Test_Extractor_Chain
(t *testing.T)
| 147 | |
| 148 | // go test -run Test_Extractor_Chain |
| 149 | func Test_Extractor_Chain(t *testing.T) { |
| 150 | t.Parallel() |
| 151 | |
| 152 | t.Run("no_extractors", func(t *testing.T) { |
| 153 | t.Parallel() |
| 154 | |
| 155 | app := fiber.New() |
| 156 | ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 157 | t.Cleanup(func() { app.ReleaseCtx(ctx) }) |
| 158 | token, err := Chain().Extract(ctx) |
| 159 | require.Empty(t, token) |
| 160 | require.ErrorIs(t, err, ErrNotFound) |
| 161 | }) |
| 162 | |
| 163 | t.Run("first_extractor_succeeds", func(t *testing.T) { |
| 164 | t.Parallel() |
| 165 | |
| 166 | app := fiber.New() |
| 167 | ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 168 | t.Cleanup(func() { app.ReleaseCtx(ctx) }) |
| 169 | ctx.Request().Header.Set("X-Token", "token_from_header") |
| 170 | ctx.Request().SetRequestURI("/?token=token_from_query") |
| 171 | token, err := Chain(FromHeader("X-Token"), FromQuery("token")).Extract(ctx) |
| 172 | require.NoError(t, err) |
| 173 | require.Equal(t, "token_from_header", token) |
| 174 | }) |
| 175 | |
| 176 | t.Run("second_extractor_succeeds", func(t *testing.T) { |
| 177 | t.Parallel() |
| 178 | |
| 179 | app := fiber.New() |
| 180 | ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 181 | t.Cleanup(func() { app.ReleaseCtx(ctx) }) |
| 182 | ctx.Request().SetRequestURI("/?token=token_from_query") |
| 183 | token, err := Chain(FromHeader("X-Token"), FromQuery("token")).Extract(ctx) |
| 184 | require.NoError(t, err) |
| 185 | require.Equal(t, "token_from_query", token) |
| 186 | }) |
| 187 | |
| 188 | t.Run("all_extractors_fail", func(t *testing.T) { |
| 189 | t.Parallel() |
| 190 | |
| 191 | app := fiber.New() |
| 192 | ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 193 | t.Cleanup(func() { app.ReleaseCtx(ctx) }) |
| 194 | token, err := Chain(FromHeader("X-Token"), FromQuery("token")).Extract(ctx) |
| 195 | require.Empty(t, token) |
| 196 | require.ErrorIs(t, err, ErrNotFound) |
| 197 | }) |
| 198 | |
| 199 | t.Run("empty_extractor_returns_not_found", func(t *testing.T) { |
| 200 | t.Parallel() |
| 201 | |
| 202 | app := fiber.New() |
| 203 | ctx := app.AcquireCtx(&fasthttp.RequestCtx{}) |
| 204 | t.Cleanup(func() { app.ReleaseCtx(ctx) }) |
| 205 | // This extractor will return "", nil |
| 206 | dummyExtractor := Extractor{ |
nothing calls this directly
no test coverage detected