<commit_before>package main

import (
	"crypto/sha1"
	"crypto/sha256"
	"crypto/hmac"
	"fmt"
	"testing"
)

func init() {
	key := "secret"
	secretKey = &key
}
func TestValidateSig(t *testing.T) {
	url := "http://mozilla.org/en-US/"
	s1 := sha1.New()
	s1.Write([]byte("secret"+ url))
	sig1 := fmt.Sprintf("%x", s1.Sum(nil))

	if !validateSig(url, sig1) {
		t.Errorf("url hash: %s did not match.", sig1)
	}

	s1 = sha1.New()
	s1.Write([]byte("badsecret"+ url))
	sig1 = fmt.Sprintf("%x", s1.Sum(nil))

	if validateSig(url, sig1) {
		t.Errorf("bad url hash: %s matched.", sig1)
	}

	h := hmac.New(sha256.New, []byte("secret"))
	h.Write([]byte(url))
	hSig := fmt.Sprintf("%x", h.Sum(nil))

	if !validateSig(url, hSig) {
		t.Errorf("url hash: %s did not match.", hSig)
	}

	h = hmac.New(sha256.New, []byte("badsecret"))
	h.Write([]byte(url))
	hSig = fmt.Sprintf("%x", h.Sum(nil))

	if validateSig(url, hSig) {
		t.Errorf("bad url hash: %s did not match.", hSig)
	}
}<commit_msg>tests: add full request test.<commit_after>package main

import (
	"crypto/sha1"
	"crypto/sha256"
	"crypto/hmac"
	"fmt"
	"net/http/httptest"
	"net/http"
	"testing"
)

func init() {
	key := "secret"
	secretKey = &key
}

func TestValidateSig(t *testing.T) {
	url := "http://mozilla.org/en-US/"
	s1 := sha1.New()
	s1.Write([]byte("secret"+ url))
	sig1 := fmt.Sprintf("%x", s1.Sum(nil))

	if !validateSig(url, sig1) {
		t.Errorf("url hash: %s did not match.", sig1)
	}

	s1 = sha1.New()
	s1.Write([]byte("badsecret"+ url))
	sig1 = fmt.Sprintf("%x", s1.Sum(nil))

	if validateSig(url, sig1) {
		t.Errorf("bad url hash: %s matched.", sig1)
	}

	h := hmac.New(sha256.New, []byte("secret"))
	h.Write([]byte(url))
	hSig := fmt.Sprintf("%x", h.Sum(nil))

	if !validateSig(url, hSig) {
		t.Errorf("url hash: %s did not match.", hSig)
	}

	h = hmac.New(sha256.New, []byte("badsecret"))
	h.Write([]byte(url))
	hSig = fmt.Sprintf("%x", h.Sum(nil))

	if validateSig(url, hSig) {
		t.Errorf("bad url hash: %s did not match.", hSig)
	}
}

func TestReq(t *testing.T) {
	h := hmac.New(sha256.New, []byte("secret"))
	h.Write([]byte("http://www.mywot.com/"))
	hSig := fmt.Sprintf("%x", h.Sum(nil))

	rec := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/v1/" + hSig + "/http%3A//www.mywot.com/", nil)
	if err != nil {
		t.Fatal(err)
	}
	readReq(rec, req)
	if rec.Code != 200 {
		t.Errorf("Expected %d, returned %d.", 200, rec.Code)
	}

	h = hmac.New(sha256.New, []byte("badsecret"))
	h.Write([]byte("http://www.mywot.com/"))
	hSig = fmt.Sprintf("%x", h.Sum(nil))

	rec = httptest.NewRecorder()
	req, err = http.NewRequest("GET", "/v1/" + hSig + "/http%3A//www.mywot.com/", nil)
	if err != nil {
		t.Fatal(err)
	}
	readReq(rec, req)
	if rec.Code != 400 {
		t.Errorf("Expected %d, returned %d.", 200, rec.Code)
	}
}
{'fraction_non_alphanumeric': 0.23018731988472624, 'fraction_numerical': 0.02521613832853026, 'mean_word_length': 3.854895104895105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, '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': '6839408', 'n_tokens_mistral': 1254, 'n_tokens_neox': 1193, 'n_words': 245}