package handler

import (
	"net/url"
	"testing"
)

func TestExportParamsRejectsMalformedNestedQuery(t *testing.T) {
	_, err := exportParams(url.Values{"params": {"name=%zz"}})
	if err == nil {
		t.Fatal("exportParams accepted malformed nested query")
	}
}

func TestExportParamsUsesFirstValue(t *testing.T) {
	got, err := exportParams(url.Values{"params": {"name=first&name=second"}})
	if err != nil {
		t.Fatalf("exportParams returned error: %v", err)
	}
	if got["name"] != "first" {
		t.Fatalf("exportParams name = %q, want first", got["name"])
	}
}

func TestExportIssueParamsKeepsBlankTemplateCompatible(t *testing.T) {
	got, err := exportIssueParams(url.Values{"params": {"name=%zz"}}, true)
	if err != nil {
		t.Fatalf("blank template params returned error: %v", err)
	}
	if len(got) != 0 {
		t.Fatalf("blank template params = %#v, want empty", got)
	}
}

func TestExportIssueParamsRejectsMalformedExcelParams(t *testing.T) {
	_, err := exportIssueParams(url.Values{"params": {"name=%zz"}}, false)
	if err == nil {
		t.Fatal("Excel export accepted malformed nested query")
	}
}
