This repository has been archived by the owner on Sep 20, 2018. It is now read-only.
forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 14
/
response_json_test.go
115 lines (88 loc) · 2.75 KB
/
response_json_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package osin
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestResponseJSON(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
r := NewResponse(NewTestingStorage())
r.Output["access_token"] = "1234"
r.Output["token_type"] = "5678"
err = OutputJSON(r, w, req)
if err != nil {
t.Fatalf("Error outputting json: %s", err)
}
//fmt.Printf("%d - %s - %+v", w.Code, w.Body.String(), w.HeaderMap)
if w.Code != 200 {
t.Fatalf("Invalid response code for output: %d", w.Code)
}
if w.HeaderMap.Get("Content-Type") != "application/json" {
t.Fatalf("Result from json must be application/json")
}
// parse output json
output := make(map[string]interface{})
if err := json.Unmarshal(w.Body.Bytes(), &output); err != nil {
t.Fatalf("Could not decode output json: %s", err)
}
if d, ok := output["access_token"]; !ok || d != "1234" {
t.Fatalf("Invalid or not found output data: access_token=%s", d)
}
if d, ok := output["token_type"]; !ok || d != "5678" {
t.Fatalf("Invalid or not found output data: token_type=%s", d)
}
}
func TestErrorResponseJSON(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
r := NewResponse(NewTestingStorage())
r.ErrorStatusCode = 500
r.SetError(E_INVALID_REQUEST, "")
err = OutputJSON(r, w, req)
if err != nil {
t.Fatalf("Error outputting json: %s", err)
}
//fmt.Printf("%d - %s - %+v", w.Code, w.Body.String(), w.HeaderMap)
if w.Code != 500 {
t.Fatalf("Invalid response code for error output: %d", w.Code)
}
if w.HeaderMap.Get("Content-Type") != "application/json" {
t.Fatalf("Result from json must be application/json")
}
// parse output json
output := make(map[string]interface{})
if err := json.Unmarshal(w.Body.Bytes(), &output); err != nil {
t.Fatalf("Could not decode output json: %s", err)
}
if d, ok := output["error"]; !ok || d != E_INVALID_REQUEST {
t.Fatalf("Invalid or not found output data: error=%s", d)
}
}
func TestRedirectResponseJSON(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost:14000/appauth", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
r := NewResponse(NewTestingStorage())
r.SetRedirect("http://localhost:14000")
err = OutputJSON(r, w, req)
if err != nil {
t.Fatalf("Error outputting json: %s", err)
}
//fmt.Printf("%d - %s - %+v", w.Code, w.Body.String(), w.HeaderMap)
if w.Code != 302 {
t.Fatalf("Invalid response code for redirect output: %d", w.Code)
}
if w.HeaderMap.Get("Location") != "http://localhost:14000" {
t.Fatalf("Invalid response location url: %s", w.HeaderMap.Get("Location"))
}
}