You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: server_test.go
    path of file: ./repos/dapr/pkg/api/http
    
    the code of the file until where you have to start completion: /*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package http

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"
	"os"
	"testing"
	"time"

	"github.com/go-chi/chi/v5"
	"github.com/phayes/freeport"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/dapr/dapr/pkg/api/http/endpoints"
	"github.com/dapr/dapr/pkg/config"
	"github.com/dapr/dapr/pkg/cors"
	dapr_testing "github.com/dapr/dapr/pkg/testing"
	"github.com/dapr/kit/logger"
)

func newServer() server {
	return server{
		config: ServerConfig{},
	}
}

func TestCorsHandler(t *testing.T) {
	hf := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
	})

	t.Run("with default cors, middleware not enabled", func(t *testing.T) {
		srv := newServer()
		srv.config.AllowedOrigins = cors.DefaultAllowedOrigins

		h := chi.NewRouter()
		srv.useCors(h)
		h.Get("/", hf)
		w := httptest.NewRecorder()
		r := httptest.NewRequest(http.MethodOptions, "/", nil)
		r.Header.Set("Origin", "*")
		h.ServeHTTP(w, r)

		assert.Empty(t, w.Header().Get("Access-Control-Allow-Origin"))
	})

	t.Run("with custom cors, middleware enabled", func(t *testing.T) {
		srv := newServer()
		srv.config.AllowedOrigins = "http://test.com"

		h := chi.NewRouter()
		srv.useCors(h)
		h.Get("/", hf)
		w := httptest.NewRecorder()
		r := httptest.NewRequest(http.MethodOptions, "/", nil)
		r.Header.Set("Origin", "http://test.com")
		h.ServeHTTP(w, r)

		assert.NotEmpty(t, w.Header().Get("Access-Control-Allow-Origin"))
	})
}

func TestUnescapeRequestParametersHandler(t *testing.T) {
	mh := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var param string
		chiCtx := chi.RouteContext(r.Context())
		if chiCtx != nil {
			param = chiCtx.URLParam("testparam")
		}
		w.Write([]byte(param))
	})

	// Create a context that has a URL parameter, which will allow us to determine if the UnescapeRequestParameters middleware was invoked
	newCtx := func() context.Context {
		chiCtx :=
    
func TestAPILogging(t *testing.T) {
	// Replace the logger with a custom one for testing
	prev := infoLog
	logDest := &bytes.Buffer{}
	infoLog = logger.NewLogger("test-api-logging")
	infoLog.EnableJSONOutput(true)
	infoLog.SetOutput(io.MultiWriter(logDest, os.Stderr))
	defer func() {
		infoLog = prev
	}()

	body := []byte("ðŸ‘‹")

	endpoints := []endpoints.Endpoint{
		{
			Methods: []string{http.MethodGet, http.MethodPost},
			Route:   "state/{storeName}/{key}",
			Version: apiVersionV1,
			Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				w.Write(body)
			}),
			Settings: endpoints.EndpointSettings{
				Name: "GetState",
			},
		},
	}
	srv := newServer()
	srv.config.EnableAPILogging = true

	router := chi.NewRouter()
	srv.useContextSetup(router)
	srv.useAPILogging(router)
	srv.setupRoutes(router, endpoints)

	dec := json.NewDecoder(logDest)

	runTest := func(userAgent string, obfuscateURL bool) func(t *testing.T) {
		return func(t *testing.T) {
			srv.config.APILoggingObfuscateURLs = obfuscateURL

			for _, e := range endpoints {
				path := fmt.Sprintf("/%s/%s", e.Version, "state/mystate/mykey")
				for _, m := range e.Methods {
					req := httptest.NewRequest(m, path, nil)
					req.Header.Set("user-agent", userAgent)
					rw := httptest.NewRecorder()

					router.ServeHTTP(rw, req)

					resp := rw.Result()
					defer resp.Body.Close()

					respBody, err := io.ReadAll(resp.Body)
					require.NoError(t, err)
					assert.Equal(t, body, respBody)

					logData := map[string]any{}
					err = dec.Decode(&logData)
					require.NoError(t, err)

					assert.Equal(t, "test-api-logging", logData["scope"])
					assert.Equal(t, "HTTP API Called", logData["msg"])

					if obfuscateURL {
						assert.Equal(t, e.Settings.Name, logData["method"])
					} else {
						assert.Equal(t, m+" "+path, logData["method"])
					}

					timeStr, ok := logData["time"].(string)
					assert.True(t, ok)
					tt, err := time.Parse(time.RFC3339Nano, timeStr)
					require.NoError(t, err)
					assert.InDelta(t, time.Now().Unix(), tt.Unix(), 120)

					// In our test the duration better be no more than 10ms!
					dur, ok := logData["duration"].(float64)
					assert.True(t, ok)
					assert.Less(t, dur, 10.0)

					assert.Equal(t, float64(len(body)), logData["size"])
					assert.Equal(t, float64(http.StatusOK), logData["code"])

					if userAgent != "" {
						assert.Equal(t, userAgent, logData["useragent"])
					} else {
						_, found := logData["useragent"]
						assert.False(t, found)
					}
				}
			}
		}
	}

	// Test user agent inclusion
	t.Run("without user agent", runTest("", false))
	t.Run("with user agent", runTest("daprtest/1", false))

	// Test obfuscate URLs
	t.Run("obfuscate URL", runTest("daprtest/1", true))
}