Beispiel #1
0
func Test_ServeHTTP_InvalidHTTPMethod(t *testing.T) {
	u := new(TestUnit)
	defer u.Teardown()
	u.Setup()

	// Setup server & test server
	server := DefaultServer(true)
	server.Get("/sample", func(c *RequestContext, s *OAuthContext) {
		c.OutputJSON(util.Status200(), map[string]string{"apple": "apple"})
	})
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		server.ServeHTTP(w, r)
	}))
	defer ts.Close()

	// Update allow methods
	Cfg.AllowMethods = []string{Get, Post, Patch, Delete}
	methodsValidation = regexp.MustCompile(fmt.Sprintf("^(%s)$", strings.Join(Cfg.AllowMethods, "|")))

	request, _ := http.NewRequest("LINK", fmt.Sprintf("%s/%s", ts.URL, "token"), nil)
	response, _ := http.DefaultClient.Do(request)
	if response.StatusCode != 405 {
		t.Errorf(test.ExpectedNumberButFoundNumber, 405, response.StatusCode)
	}
}
Beispiel #2
0
func Test_ServeHTTP_ValidURL(t *testing.T) {
	u := new(TestUnit)
	defer u.Teardown()
	u.Setup()

	// Setup server & test server
	server := DefaultServer(true)
	server.Get("/sample", func(c *RequestContext, s *OAuthContext) {
		c.OutputJSON(util.Status200(), map[string]string{"apple": "apple"})
	})
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		server.ServeHTTP(w, r)
	}))
	defer ts.Close()

	response, _ := http.Get(fmt.Sprintf("%s/%s", ts.URL, "sample"))
	if response.StatusCode != 200 {
		t.Errorf(test.ExpectedNumberButFoundNumber, 200, response.StatusCode)
	} else {
		bytes, _ := ioutil.ReadAll(response.Body)

		if string(bytes) != "{\"apple\":\"apple\"}" {
			t.Errorf(test.ExpectedStringButFoundString, "{\"apple\":\"apple\"}", string(bytes))
		}
	}
}
Beispiel #3
0
func Test_OutputHeader(t *testing.T) {
	// Create test server
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		context := createRequestContext(r, w)

		context.OutputHeader("test-header", "test-header-value")
		context.OutputError(util.Status200())
	}))
	defer ts.Close()

	response, _ := http.Post(ts.URL, "application/x-www-form-urlencoded", nil)
	if response.Header.Get("test-header") != "test-header-value" {
		t.Errorf(test.ExpectedStringButFoundString, "test-header-value", response.Header.Get("test-header"))
	}
}
Beispiel #4
0
func Test_OutputText(t *testing.T) {
	// Create test server
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		context := createRequestContext(r, w)
		context.OutputText(util.Status200(), "Sample test!")
	}))
	defer ts.Close()

	response, _ := http.Get(ts.URL)
	bytes, _ := ioutil.ReadAll(response.Body)

	if response.StatusCode != 200 {
		t.Errorf(test.ExpectedNumberButFoundNumber, 200, response.StatusCode)
	}
	if string(bytes) != "Sample test!" {
		t.Errorf(test.ExpectedStringButFoundString, "Sample test!", string(bytes))
	}
}
Beispiel #5
0
func Test_ServeHTTP_ValidResource(t *testing.T) {
	u := new(TestUnit)
	defer u.Teardown()
	u.Setup()

	// Setup server & test server
	server := DefaultServer(true)
	server.Get("/sample", func(c *RequestContext, s *OAuthContext) {
		c.OutputJSON(util.Status200(), map[string]string{"apple": "apple"})
	})
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		server.ServeHTTP(w, r)
	}))
	defer ts.Close()

	response, _ := http.Get(fmt.Sprintf("%s/%s", ts.URL, "resources/LICENSE"))
	if response.StatusCode != 200 {
		t.Errorf(test.ExpectedNumberButFoundNumber, 200, response.StatusCode)
	}
}
Beispiel #6
0
func Test_ServeHTTP_InvalidURL(t *testing.T) {
	u := new(TestUnit)
	defer u.Teardown()
	u.Setup()

	// Setup server & test server
	server := DefaultServer(true)
	server.Get("/sample", func(c *RequestContext, s *OAuthContext) {
		c.OutputJSON(util.Status200(), map[string]string{"apple": "apple"})
	})
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		server.ServeHTTP(w, r)
	}))
	defer ts.Close()

	request, _ := http.NewRequest("POST", fmt.Sprintf("%s/%s", ts.URL, "sample"), strings.NewReader(""))
	request.Header.Set("content-type", "application/x-www-form-urlencoded")

	response, _ := http.DefaultClient.Do(request)
	if response.StatusCode != 503 {
		t.Errorf(test.ExpectedNumberButFoundNumber, 503, response.StatusCode)
	}
}
Beispiel #7
0
func Test_BindJSON(t *testing.T) {
	// Create test server
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		context := createRequestContext(r, w)

		status := new(util.Status)
		context.BindJSON(status)

		if status.Code != 200 {
			t.Errorf(test.ExpectedNumberButFoundNumber, 200, status.Code)
		}
		if status.Description != http.StatusText(200) {
			t.Errorf(test.ExpectedStringButFoundString, http.StatusText(200), status.Description)
		}
	}))
	defer ts.Close()
	b, _ := json.Marshal(util.Status200())

	request, _ := http.NewRequest("POST", ts.URL, bytes.NewBuffer(b))
	request.Header.Set("Content-Type", "application/json")

	client := http.DefaultClient
	client.Do(request)
}