<commit_before>package routes

import (
	"log"
	"net/http"
	"testing"

	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
	"github.com/stretchr/testify/assert"
)

// helloWorldMiddleware is a test middleware that does nothing
type helloWorldMiddleware struct{}

// ServeHTTP as per the negroni.Handler interface
func (m *helloWorldMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
}

func TestAddRoutes(t *testing.T) {
	router := mux.NewRouter().StrictSlash(true)

	// Add a test GET route without a middleware
	AddRoutes([]Route{
		Route{
			Name:        "foobar_route",
			Methods:     []string{"GET"},
			Pattern:     "/bar",
			HandlerFunc: func(w http.ResponseWriter, r *http.Request) {},
		},
	}, router.PathPrefix("/foo").Subrouter())

	// Add a test POST route with a middleware
	AddRoutes([]Route{
		Route{
			Name:        "helloworld_route",
			Methods:     []string{"POST"},
			Pattern:     "/world",
			HandlerFunc: func(w http.ResponseWriter, r *http.Request) {},
			Middlewares: []negroni.Handler{
				new(helloWorldMiddleware),
			},
		},
	}, router.PathPrefix("/hello").Subrouter())

	var match *mux.RouteMatch
	var r *http.Request
	var err error

	// Test the foobar_route
	r, err = http.NewRequest("GET", "http://1.2.3.4/foo/bar", nil)
	if err != nil {
		log.Fatal(err)
	}
	match = new(mux.RouteMatch)
	router.Match(r, match)
	assert.Equal(t, "foobar_route", match.Route.GetName())

	// Test the helloworld_route
	r, err = http.NewRequest("POST", "http://1.2.3.4/hello/world", nil)
	if err != nil {
		log.Fatal(err)
	}
	match = new(mux.RouteMatch)
	router.Match(r, match)
	assert.Equal(t, "helloworld_route", match.Route.GetName())
	// TODO - test the helloWorldMiddleware was added to the route
}
<commit_msg>Improved routes test to check middleware registration.<commit_after>package routes

import (
	"log"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/codegangsta/negroni"
	"github.com/gorilla/mux"
	"github.com/stretchr/testify/assert"
)

// helloWorldMiddleware is a test middleware that writes "hello world" to the
// response so we can check the middleware is registered
type helloWorldMiddleware struct{}

// ServeHTTP as per the negroni.Handler interface
func (m *helloWorldMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	w.Write([]byte("hello world"))
	next(w, r)
}

func TestAddRoutes(t *testing.T) {
	router := mux.NewRouter().StrictSlash(true)

	// Add a test GET route without a middleware
	AddRoutes([]Route{
		Route{
			Name:        "foobar_route",
			Methods:     []string{"GET"},
			Pattern:     "/bar",
			HandlerFunc: func(w http.ResponseWriter, r *http.Request) {},
		},
	}, router.PathPrefix("/foo").Subrouter())

	// Add a test POST route with a middleware
	AddRoutes([]Route{
		Route{
			Name:        "helloworld_route",
			Methods:     []string{"POST"},
			Pattern:     "/world",
			HandlerFunc: func(w http.ResponseWriter, r *http.Request) {},
			Middlewares: []negroni.Handler{
				new(helloWorldMiddleware),
			},
		},
	}, router.PathPrefix("/hello").Subrouter())

	var match *mux.RouteMatch
	var r *http.Request
	var w *httptest.ResponseRecorder
	var err error

	// Test the foobar_route
	r, err = http.NewRequest("GET", "http://1.2.3.4/foo/bar", nil)
	if err != nil {
		log.Fatal(err)
	}
	match = new(mux.RouteMatch)
	router.Match(r, match)
	assert.Equal(t, "foobar_route", match.Route.GetName())
	// Test no middleware has been registered
	w = httptest.NewRecorder()
	match.Route.GetHandler().ServeHTTP(w, r)
	assert.Equal(t, "", w.Body.String())

	// Test the helloworld_route is correctly matched
	r, err = http.NewRequest("POST", "http://1.2.3.4/hello/world", nil)
	if err != nil {
		log.Fatal(err)
	}
	match = new(mux.RouteMatch)
	router.Match(r, match)
	assert.Equal(t, "helloworld_route", match.Route.GetName())
	// Test the helloWorldMiddleware has been registered
	w = httptest.NewRecorder()
	match.Route.GetHandler().ServeHTTP(w, r)
	assert.Equal(t, "hello world", w.Body.String())
}

{'fraction_non_alphanumeric': 0.15045780747339768, 'fraction_numerical': 0.003959415986142044, 'mean_word_length': 3.9292682926829268, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '4514949', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1415, 'n_words': 332}