// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0-or-later

package negotiation

import (
	"net/http"
	"testing"
)

func TestContentType(t *testing.T) {

	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}

	req.Header.Set("Content-Type", "application/json")

	n := New(req.Header)

	if n.ContentType("application/json") != "application/json" {
		t.Errorf("handler returned wrong status code: got %v want %v",
			n.Type("application/json"), "application/json")
	}

}

func TestNegotiationLanguage(t *testing.T) {

	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}

	req.Header.Set("Accept-Language", "en-US;q=0.8, en;q=0.7")

	n := New(req.Header)

	if n.Language("en-GB", "en") != "en" {
		t.Errorf("accepted locale should be %v, got %v", "en", "en-GB")
	}

	if n.Language("en-GB") != "" {
		t.Errorf("there should be no accepted locale, got %v", n.Language("en-GB"))
	}

	if n.Language("en-US") != "en-US" {
		t.Errorf("accepted locale should be %v, got %v", "en-US", "en-GB")
	}

}

func TestNegotiationCharset(t *testing.T) {

	req, err := http.NewRequest("GET", "/config", nil)
	if err != nil {
		t.Fatal(err)
	}

	req.Header.Set("Accept-Charset", "utf-8")

	n := New(req.Header)

	if n.Charset("utf-8") != "utf-8" {
		t.Errorf("accepted charset should be %v, got %v", "utf-8", "utf-8")
	}

}

func TestNegotiationEncoding(t *testing.T) {

	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}

	req.Header.Set("Accept-Encoding", "gzip")

	n := New(req.Header)

	if n.Encoding("gzip") != "gzip" {
		t.Errorf("accepted encoding should be %v, got %v", "gzip", "gzip")
	}

}

func TestNegotiationType(t *testing.T) {

	req, err := http.NewRequest("GET", "/", nil)
	if err != nil {
		t.Fatal(err)
	}

	req.Header.Set("Accept", "application/yaml")

	n := New(req.Header)

	if n.Type("application/json") != "" {
		t.Errorf("handler returned wrong status code: got %v want %v",
			n.Type("application/json"), "application/yaml")
	}

	if n.Type("application/yaml") != "application/yaml" {
		t.Errorf("handler returned wrong status code: got %v want %v",
			n.Type("application/yaml"), "application/yaml")
	}

}