summary refs log tree commit diff stats
path: root/svc
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-09 15:00:02 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-09 15:25:05 -0400
commita9d7d114e9ed0b5b4157a13b9a90e175f2785584 (patch)
treeb16dfbee0906324133a382ea2aa04235b631d7b1 /svc
parent2fd6b1aaa6b8382fabf1361259188f4ef36472d3 (diff)
downloadgetwtxt-a9d7d114e9ed0b5b4157a13b9a90e175f2785584.tar.gz
apiEndpointHandler now 404s invalid endpoints, better testing for it
Diffstat (limited to 'svc')
-rw-r--r--svc/handlers.go6
-rw-r--r--svc/handlers_test.go71
2 files changed, 68 insertions, 9 deletions
diff --git a/svc/handlers.go b/svc/handlers.go
index 5bb0d4f..02975ef 100644
--- a/svc/handlers.go
+++ b/svc/handlers.go
@@ -133,9 +133,13 @@ func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
 		out, err = twtxtCache.QueryInStatus("@<")
 		out = registry.ReduceToPage(page, out)
 
-	default:
+	case "/api/plain/tweets":
 		out, err = twtxtCache.QueryAllStatuses()
 		out = registry.ReduceToPage(page, out)
+
+	default:
+		log404(w, r, fmt.Errorf("endpoint not found"))
+		return
 	}
 	errLog("", err)
 
diff --git a/svc/handlers_test.go b/svc/handlers_test.go
index 36c795a..fa73de5 100644
--- a/svc/handlers_test.go
+++ b/svc/handlers_test.go
@@ -52,17 +52,72 @@ func Test_apiFormatHandler(t *testing.T) {
 		}
 	})
 }
+
+var endpointCases = []struct {
+	name   string
+	req    *http.Request
+	status int
+}{
+	{
+		name:   "Regular Query: /api/plain/users",
+		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/users", nil),
+		status: http.StatusOK,
+	},
+	{
+		name:   "Regular Query: /api/plain/mentions",
+		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/mentions", nil),
+		status: http.StatusOK,
+	},
+	{
+		name:   "Regular Query: /api/plain/tweets",
+		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/tweets", nil),
+		status: http.StatusOK,
+	},
+	{
+		name:   "Invalid Endpoint: /api/plain/statuses",
+		req:    httptest.NewRequest("GET", "http://localhost"+testport+"/api/plain/statuses", nil),
+		status: http.StatusNotFound,
+	},
+}
+
 func Test_apiEndpointHandler(t *testing.T) {
 	initTestConf()
-	t.Run("apiEndpointHandler", func(t *testing.T) {
-		w := httptest.NewRecorder()
-		req := httptest.NewRequest("GET", "localhost"+testport+"/api/plain/users", nil)
-		apiEndpointHandler(w, req)
-		resp := w.Result()
-		if resp.StatusCode != http.StatusOK {
-			t.Errorf(fmt.Sprintf("%v", resp.StatusCode))
+	mockRegistry()
+	for _, tt := range endpointCases {
+		t.Run(tt.name, func(t *testing.T) {
+			w := httptest.NewRecorder()
+			apiEndpointHandler(w, tt.req)
+			resp := w.Result()
+			if resp.StatusCode != tt.status {
+				t.Errorf(fmt.Sprintf("%v", resp.StatusCode))
+			}
+			if tt.status == http.StatusOK {
+				var body []byte
+				buf := bytes.NewBuffer(body)
+				err := resp.Write(buf)
+				if err != nil {
+					t.Errorf("%v\n", err)
+				}
+				if buf == nil {
+					t.Errorf("Got nil\n")
+				}
+				if len(buf.Bytes()) == 0 {
+					t.Errorf("Got zero data\n")
+				}
+			}
+		})
+	}
+}
+func Benchmark_apiEndpointHandler(b *testing.B) {
+	initTestConf()
+	mockRegistry()
+	w := httptest.NewRecorder()
+	b.ResetTimer()
+	for _, tt := range endpointCases {
+		for i := 0; i < b.N; i++ {
+			apiEndpointHandler(w, tt.req)
 		}
-	})
+	}
 }
 
 func Test_apiTagsBaseHandler(t *testing.T) {
ass='alt'>
3cd8a402 ^
b3d031a9 ^
397106b6 ^
d8b807c5 ^



397106b6 ^
d8b807c5 ^


397106b6 ^

d8b807c5 ^

397106b6 ^
d8b807c5 ^
397106b6 ^

31277e06 ^

20f0f2ba ^












144de554 ^


d1a1173d ^





03dae5f0 ^
fc5648cd ^





















144de554 ^
20f0f2ba ^

397106b6 ^



3b96a37f ^
6bc13af0 ^












1a6d1846 ^



3cd8a402 ^
76791a70 ^

6bc13af0 ^

76791a70 ^


6bc13af0 ^







fe5f90d3 ^
6bc13af0 ^



144de554 ^



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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147