<commit_before>package main

import (
	"color-assert"
	"encoding/json"
	"fakehttp"
	"fmt"
	"iiif"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func rootDir() string {
	p, _ := os.Getwd()
	root, _ := filepath.Abs(p + "/../../../")
	return root
}

// Sets up everything necessary to test an IIIF request
func dorequest(path string, acceptLD bool, t *testing.T) *fakehttp.ResponseWriter {
	u, _ := url.Parse("http://example.com/foo/bar")
	w := fakehttp.NewResponseWriter()
	reqPath := fmt.Sprintf("/foo/bar/%s", path)
	req, err := http.NewRequest("get", reqPath, strings.NewReader(""))
	req.RequestURI = reqPath

	if acceptLD {
		req.Header.Add("Accept", "application/ld+json")
	}

	if err != nil {
		t.Errorf("Unable to create fake request: %s", err)
	}
	h := NewImageHandler(u, rootDir())
	h.FeatureSet = iiif.FeatureSet1()
	h.Route(w, req)

	return w
}

func request(path string, t *testing.T) *fakehttp.ResponseWriter {
	return dorequest(path, false, t)
}

func requestLD(path string, t *testing.T) *fakehttp.ResponseWriter {
	return dorequest(path, true, t)
}

func TestInfoHandler404(t *testing.T) {
	w := request("identifier/info.json", t)
	assert.Equal(404, w.StatusCode, "Invalid info request returns 404", t)
}

func TestInfoHandler(t *testing.T) {
	w := request("testfile%2Ftest-world.jp2/info.json", t)
	assert.Equal(-1, w.StatusCode, "Valid info request doesn't explicitly set status code", t)
	var data iiif.Info
	json.Unmarshal(w.Output, &data)
	assert.Equal("http://iiif.io/api/image/2/level1.json", data.Profile[0], "Proper profile string", t)
	assert.Equal(800, data.Width, "JSON-decoded width", t)
	assert.Equal(400, data.Height, "JSON-decoded height", t)
	assert.Equal(800, data.Tiles[0].Width, "JSON-decoded tile width", t)
	assert.Equal(400, data.Tiles[0].Height, "JSON-decoded tile height", t)<commit_msg>Fix tests<commit_after>package main

import (
	"color-assert"
	"encoding/json"
	"fakehttp"
	"fmt"
	"iiif"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func rootDir() string {
	p, _ := os.Getwd()
	root, _ := filepath.Abs(p + "/../../../")
	return root
}

// Sets up everything necessary to test an IIIF request
func dorequest(path string, acceptLD bool, t *testing.T) *fakehttp.ResponseWriter {
	u, _ := url.Parse("http://example.com/foo/bar")
	w := fakehttp.NewResponseWriter()
	reqPath := fmt.Sprintf("/foo/bar/%s", path)
	req, err := http.NewRequest("get", reqPath, strings.NewReader(""))
	req.RequestURI = reqPath

	if acceptLD {
		req.Header.Add("Accept", "application/ld+json")
	}

	if err != nil {
		t.Errorf("Unable to create fake request: %s", err)
	}
	h := NewImageHandler(rootDir())
	h.EnableIIIF(u)
	h.FeatureSet = iiif.FeatureSet1()
	h.IIIFRoute(w, req)

	return w
}

func request(path string, t *testing.T) *fakehttp.ResponseWriter {
	return dorequest(path, false, t)
}

func requestLD(path string, t *testing.T) *fakehttp.ResponseWriter {
	return dorequest(path, true, t)
}

func TestInfoHandler404(t *testing.T) {
	w := request("identifier/info.json", t)
	assert.Equal(404, w.StatusCode, "Invalid info request returns 404", t)
}

func TestInfoHandler(t *testing.T) {
	w := request("testfile%2Ftest-world.jp2/info.json", t)
	assert.Equal(-1, w.StatusCode, "Valid info request doesn't explicitly set status code", t)
	var data iiif.Info
	json.Unmarshal(w.Output, &data)
	assert.Equal("http://iiif.io/api/image/2/level1.json", data.Profile[0], "Proper profile string", t)
	assert.Equal(800, data.Width, "JSON-decoded width", t)
	assert.Equal(400, data.Height, "JSON-decoded height", t)
	assert.Equal(800, data.Tiles[0].Width, "JSON-decoded tile width", t)
	assert.Equal(400, data.Tiles[0].Height, "JSON-decoded tile height", t)
{'fraction_non_alphanumeric': 0.17007534983853606, 'fraction_numerical': 0.016146393972012917, 'mean_word_length': 4.514836795252226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3301016', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1380, 'n_words': 322}