<commit_before>package web;

import (
	"testing"
	"net/url"
	"fmt"
	"net/http"
	"strings"
)

func TestURLDatasource(t *testing.T) {
	rawurl := "http://user:password@example.com/path?foo=bar#fragment"
	testUrl, err := url.Parse(rawurl)

	if err != nil {
		t.Error("! Unexpected error.", err)
		return
	}
	ds := new(URLDatasource)
	ds.Init(testUrl)

	// Test the string values.
	arr := map[string]string{
		"scheme": "http",
		"Path": "/path",
		"host": "example.com",
		"fragment": "fragment",
	}
	for key, val := range arr {
		if ds.Value(key) != val {
			t.Error(fmt.Sprintf("! Expected '%s', got '%s'", val, ds.Value(key)))
		}
	}

	// Test the Query Values object.
	qvals := ds.Value("Query").(url.Values)
	if qvals.Get("foo") != "bar" {
		t.Error("! Expected to find foo=bar query param. Found ", qvals["foo"])
	}

	// Test the Userinfo object
	uinfo, ok := ds.Value("User").(url.Userinfo)
	if (!ok) {
		t.Error("Expected a Userinfo object.")
	}
	if uinfo.Username() != "user" {
		t.Error("! Expected user name 'user', got ", uinfo.Username)
	}
}

func TestQueryParameterDatasource (t *testing.T) {
	testUrl, err := url.ParseRequestURI("/foo?a=b&c=foo+bar&d=1234&d=5678")
	if err != nil {
		t.Error("! Unexpected URL parse error.")
	}
	ds := new(QueryParameterDatasource).Init(testUrl.Query())

	// Test the string values.
	arr := map[string]string{
		"a": "b",
		"c": "foo bar",
		"d": "5678",
	}
	for key, val := range arr {
		if ds.Value(key) != val {
			t.Error(fmt.Sprintf("! Expected '%s', got '%s'", val, ds.Value(key)))
		}
	}

}

func TestFormValuesDatasource(t *testing.T) {
	method := "POST"
	urlString := "http://example.com/form/test"
	body := strings.NewReader("name=Inigo+Montoya&fingers=6")

	request, err := http.NewRequest(method, urlString, body)

	// Canary
	if err != nil {
		t.Error("! Error constructing a request.", err)
	}

	ds := new(FormValuesDatasource).Init(request)

	if ds.Value("name").(string) != "<NAME>" {
		t.Error("! Prepare to die.")
	}

	if ds.Value("fingers") != 6 {
		t.Error("! Expected six fingers, but got less.")
	}
}

func TestPathDatasource(t *testing.T) {
	ds := new(PathDatasource).Init("/foo/bar")
	if ds.Value("1") != "foo" {
		t.Error("! Expected value 1 to be 'foo'. Got ", ds.Value("1"))
	}

}<commit_msg>Updated tests to ensure this works with HTTP verbs.<commit_after>package web;

import (
	"testing"
	"net/url"
	"fmt"
	"net/http"
	"strings"
)

func TestURLDatasource(t *testing.T) {
	rawurl := "http://user:password@example.com/path?foo=bar#fragment"
	testUrl, err := url.Parse(rawurl)

	if err != nil {
		t.Error("! Unexpected error.", err)
		return
	}
	ds := new(URLDatasource)
	ds.Init(testUrl)

	// Test the string values.
	arr := map[string]string{
		"scheme": "http",
		"Path": "/path",
		"host": "example.com",
		"fragment": "fragment",
	}
	for key, val := range arr {
		if ds.Value(key) != val {
			t.Error(fmt.Sprintf("! Expected '%s', got '%s'", val, ds.Value(key)))
		}
	}

	// Test the Query Values object.
	qvals := ds.Value("Query").(url.Values)
	if qvals.Get("foo") != "bar" {
		t.Error("! Expected to find foo=bar query param. Found ", qvals["foo"])
	}

	// Test the Userinfo object
	uinfo, ok := ds.Value("User").(url.Userinfo)
	if (!ok) {
		t.Error("Expected a Userinfo object.")
	}
	if uinfo.Username() != "user" {
		t.Error("! Expected user name 'user', got ", uinfo.Username)
	}
}

func TestQueryParameterDatasource (t *testing.T) {
	testUrl, err := url.ParseRequestURI("/foo?a=b&c=foo+bar&d=1234&d=5678")
	if err != nil {
		t.Error("! Unexpected URL parse error.")
	}
	ds := new(QueryParameterDatasource).Init(testUrl.Query())

	// Test the string values.
	arr := map[string]string{
		"a": "b",
		"c": "foo bar",
		"d": "5678",
	}
	for key, val := range arr {
		if ds.Value(key) != val {
			t.Error(fmt.Sprintf("! Expected '%s', got '%s'", val, ds.Value(key)))
		}
	}

}

func TestFormValuesDatasource(t *testing.T) {
	method := "POST"
	urlString := "http://example.com/form/test"
	body := strings.NewReader("name=Inigo+Montoya&fingers=6")

	request, err := http.NewRequest(method, urlString, body)

	// Canary
	if err != nil {
		t.Error("! Error constructing a request.", err)
	}

	ds := new(FormValuesDatasource).Init(request)

	if ds.Value("name").(string) != "<NAME>" {
		t.Error("! Prepare to die.")
	}

	if ds.Value("fingers") != 6 {
		t.Error("! Expected six fingers, but got less.")
	}
}

func TestPathDatasource(t *testing.T) {
	ds := new(PathDatasource).Init("/foo/bar")
	if ds.Value("1") != "foo" {
		t.Error("! Expected value 1 to be 'foo'. Got ", ds.Value("1"))
	}

	if ds.Value("2") != "bar" {
		t.Error("! Expected value 2 to be 'bar'. Got ", ds.Value("1"))
	}

	ds = new(PathDatasource).Init("POST /foo/bar")
	if ds.Value("1") != "foo" {
		t.Error("! Expected value 1 to be 'foo'. Got ", ds.Value("1"))
	}

	if ds.Value("2") != "bar" {
		t.Error("! Expected value 2 to be 'bar'. Got ", ds.Value("1"))
	}

	ds = new(PathDatasource).Init("POST /foo/bar/baz/a/b/c/d/e/f")
	if ds.Value("1") != "foo" {
		t.Error("! Expected value 1 to be 'foo'. Got ", ds.Value("1"))
	}

	if ds.Value("2") != "bar" {
		t.Error("! Expected value 2 to be 'bar'. Got ", ds.Value("1"))
	}
}
{'fraction_non_alphanumeric': 0.20948693126815102, 'fraction_numerical': 0.009486931268151016, 'mean_word_length': 3.700636942675159, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2815401', 'n_tokens_mistral': 2138, 'n_tokens_neox': 2025, 'n_words': 480}