<commit_before>package httpcache

import (
	"github.com/bmizerany/assert"
	"github.com/lostisland/go-sawyer"
	"github.com/lostisland/go-sawyer/mediatype"
	"io/ioutil"
	"net/http"
	"net/url"
	"testing"
)

func TestGetMissingCache(t *testing.T) {
	req := request()
	cache := NewMemoryCache()
	res := cache.Get(req, nil)
	if res != nil {
		t.Fatal("response was found")
	}
}

func TestGetCacheWithoutValue(t *testing.T) {
	orig := &sawyer.Response{Response: &http.Response{StatusCode: 1}}

	req := request()
	cache := NewMemoryCache()
	cache.Set(req, orig, nil)

	res := cache.Get(req, nil)
	if res == nil {
		t.Fatal("Response is nil")
	}

	assert.Equal(t, false, res.IsError())
	assert.Equal(t, 1, res.StatusCode)
}

func TestSetAndGetCache(t *testing.T) {
	mt, err := mediatype.Parse("application/json")
	assert.Equal(t, nil, err)

	testOrig := &TestResource{2}
	body, err := mt.Encode(testOrig)

	orig := &sawyer.Response{
		MediaType: mt,
		Response: &http.Response{
			StatusCode:    1,
			ContentLength: int64(body.Len()),
			Body:          ioutil.NopCloser(body),
		},
	}

	req := request()
	cache := NewMemoryCache()
	err = cache.Set(req, orig, testOrig)<commit_msg>fix memory test<commit_after>package httpcache

import (
	"github.com/bmizerany/assert"
	"github.com/lostisland/go-sawyer"
	"github.com/lostisland/go-sawyer/mediatype"
	"io/ioutil"
	"net/http"
	"net/url"
	"testing"
)

func TestGetMissingCache(t *testing.T) {
	req := request()
	cache := NewMemoryCache()
	res := cache.Get(req, nil)
	assert.Equal(t, true, res.IsError(), "response was found")
}

func TestGetCacheWithoutValue(t *testing.T) {
	orig := &sawyer.Response{Response: &http.Response{StatusCode: 1}}

	req := request()
	cache := NewMemoryCache()
	cache.Set(req, orig, nil)

	res := cache.Get(req, nil)
	assert.Equal(t, false, res.IsError(), "response was not found")
	assert.Equal(t, 1, res.StatusCode)
}

func TestSetAndGetCache(t *testing.T) {
	mt, err := mediatype.Parse("application/json")
	assert.Equal(t, nil, err)

	testOrig := &TestResource{2}
	body, err := mt.Encode(testOrig)

	orig := &sawyer.Response{
		MediaType: mt,
		Response: &http.Response{
			StatusCode:    1,
			ContentLength: int64(body.Len()),
			Body:          ioutil.NopCloser(body),
		},
	}

	req := request()
	cache := NewMemoryCache()
	err = cache.Set(req, orig, testOrig)
{'fraction_non_alphanumeric': 0.16566523605150216, 'fraction_numerical': 0.005150214592274678, 'mean_word_length': 4.309794988610478, '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': '7426862', 'n_tokens_mistral': 940, 'n_tokens_neox': 891, 'n_words': 177}