package httputil

import (
	"net/http/httptest"
	"testing"
)

func TestExtractBearerToken(t *testing.T) {
	tests := []struct {
		header string
		want   string
	}{
		{"Bearer mytoken123", "mytoken123"},
		{"bearer MYTOKEN", "MYTOKEN"},
		{"", ""},
		{"Basic abc", ""},
		{"Bearer ", ""},
	}
	for _, tc := range tests {
		req := httptest.NewRequest("GET", "/", nil)
		if tc.header != "" {
			req.Header.Set("Authorization", tc.header)
		}
		got := ExtractBearerToken(req)
		if got != tc.want {
			t.Errorf("ExtractBearerToken(%q) = %q, want %q", tc.header, got, tc.want)
		}
	}
}

func TestWriteJSON(t *testing.T) {
	w := httptest.NewRecorder()
	WriteJSON(w, 200, map[string]string{"ok": "true"}, nil)

	if w.Code != 200 {
		t.Errorf("expected 200, got %d", w.Code)
	}
	if w.Header().Get("Content-Type") != "application/json" {
		t.Errorf("expected application/json, got %s", w.Header().Get("Content-Type"))
	}
}

func TestWriteJSONWithExtraHeaders(t *testing.T) {
	w := httptest.NewRecorder()
	WriteJSON(w, 201, nil, map[string]string{"X-Custom": "value"})

	if w.Header().Get("X-Custom") != "value" {
		t.Errorf("expected X-Custom=value, got %s", w.Header().Get("X-Custom"))
	}
}
