Skip to content

Commit e8e44f8

Browse files
committed
fix: add mime parser for validations
1 parent 79dd48b commit e8e44f8

2 files changed

Lines changed: 62 additions & 13 deletions

File tree

backend/internal/api/validator/validator.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"log"
7+
"mime"
78
"net/http"
89
"strconv"
910
"strings"
@@ -36,6 +37,17 @@ func formatpath(path string) []string {
3637
return strings.Split(path, "/")
3738
}
3839

40+
func validateJSONContentType(r *http.Request) *httpError {
41+
contentType := r.Header.Get("Content-Type")
42+
mediaType, _, err := mime.ParseMediaType(contentType)
43+
if err != nil || mediaType != "application/json" {
44+
log.Printf("invalid content-type: %s", contentType)
45+
return &httpError{Status: http.StatusUnsupportedMediaType, Msg: "media not supported"}
46+
}
47+
48+
return nil
49+
}
50+
3951
func GetNoteValidator(r *http.Request) (int, *httpError) {
4052
if err := validatehttpmethod(r, http.MethodGet); err != nil {
4153
return 0, &httpError{Status: http.StatusBadRequest, Msg: "method is not supported"}
@@ -110,10 +122,8 @@ func UpdateNoteValidator(r *http.Request) (int, *httpError) {
110122
return 0, &httpError{Status: http.StatusMethodNotAllowed, Msg: "method not supported"}
111123
}
112124

113-
contentType := r.Header.Get("Content-Type")
114-
if contentType != "application/json" {
115-
log.Printf("invalid content-type: %s", contentType)
116-
return 0, &httpError{Status: http.StatusUnsupportedMediaType, Msg: "media not supported"}
125+
if err := validateJSONContentType(r); err != nil {
126+
return 0, err
117127
}
118128

119129
if r.ContentLength == 0 {
@@ -141,10 +151,8 @@ func ImportNotesValidator(r *http.Request) *httpError {
141151
return &httpError{Status: http.StatusMethodNotAllowed, Msg: "method not supported"}
142152
}
143153

144-
contentType := r.Header.Get("Content-Type")
145-
if contentType != "application/json" {
146-
log.Printf("invalid content-type: %s", contentType)
147-
return &httpError{Status: http.StatusUnsupportedMediaType, Msg: "media not supported"}
154+
if err := validateJSONContentType(r); err != nil {
155+
return err
148156
}
149157

150158
parts := formatpath(r.URL.Path)

backend/internal/api/validator/validator_test.go

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
/*
13-
GetNoteValidator
13+
GetNoteValidator
1414
*/
1515
func TestGetNoteValidator_Success(t *testing.T) {
1616
req := httptest.NewRequest(http.MethodGet, "/notes/1", nil)
@@ -30,7 +30,7 @@ func TestGetNoteValidator_InvalidMethod(t *testing.T) {
3030
}
3131

3232
/*
33-
GetNotelistValidator
33+
GetNotelistValidator
3434
*/
3535
func TestGetNotelistValidator_Success(t *testing.T) {
3636
req := httptest.NewRequest(http.MethodGet, "/notes", nil)
@@ -49,7 +49,7 @@ func TestGetNotelistValidator_InvalidPath(t *testing.T) {
4949
}
5050

5151
/*
52-
CreateNoteValidator
52+
CreateNoteValidator
5353
*/
5454
func TestCreateNoteValidator_Success(t *testing.T) {
5555
req := httptest.NewRequest(http.MethodPost, "/notes", nil)
@@ -68,7 +68,7 @@ func TestCreateNoteValidator_InvalidMethod(t *testing.T) {
6868
}
6969

7070
/*
71-
DeleteNoteValidator
71+
DeleteNoteValidator
7272
*/
7373
func TestDeleteNoteValidator_Success(t *testing.T) {
7474
req := httptest.NewRequest(http.MethodDelete, "/notes/2", nil)
@@ -88,7 +88,7 @@ func TestDeleteNoteValidator_InvalidID(t *testing.T) {
8888
}
8989

9090
/*
91-
UpdateNoteValidator
91+
UpdateNoteValidator
9292
*/
9393
func TestUpdateNoteValidator_Success(t *testing.T) {
9494
body := strings.NewReader(`{"title":"test"}`)
@@ -101,6 +101,17 @@ func TestUpdateNoteValidator_Success(t *testing.T) {
101101
assert.Equal(t, 3, id)
102102
}
103103

104+
func TestUpdateNoteValidator_AcceptsContentTypeParameters(t *testing.T) {
105+
body := strings.NewReader(`{"title":"test"}`)
106+
req := httptest.NewRequest(http.MethodPatch, "/notes/3", body)
107+
req.Header.Set("Content-Type", "application/json; charset=utf-8")
108+
109+
id, err := UpdateNoteValidator(req)
110+
111+
assert.Nil(t, err)
112+
assert.Equal(t, 3, id)
113+
}
114+
104115
func TestUpdateNoteValidator_InvalidContentType(t *testing.T) {
105116
body := strings.NewReader(`{"title":"test"}`)
106117
req := httptest.NewRequest(http.MethodPatch, "/notes/3", body)
@@ -110,3 +121,33 @@ func TestUpdateNoteValidator_InvalidContentType(t *testing.T) {
110121

111122
assert.NotNil(t, err)
112123
}
124+
125+
func TestImportNotesValidator_Success(t *testing.T) {
126+
body := strings.NewReader(`{"title":"test","content":{}}`)
127+
req := httptest.NewRequest(http.MethodPost, "/notes/imports", body)
128+
req.Header.Set("Content-Type", "application/json")
129+
130+
err := ImportNotesValidator(req)
131+
132+
assert.Nil(t, err)
133+
}
134+
135+
func TestImportNotesValidator_AcceptsContentTypeParameters(t *testing.T) {
136+
body := strings.NewReader(`{"title":"test","content":{}}`)
137+
req := httptest.NewRequest(http.MethodPost, "/notes/imports", body)
138+
req.Header.Set("Content-Type", "application/json; charset=utf-8")
139+
140+
err := ImportNotesValidator(req)
141+
142+
assert.Nil(t, err)
143+
}
144+
145+
func TestImportNotesValidator_InvalidContentType(t *testing.T) {
146+
body := strings.NewReader(`{"title":"test","content":{}}`)
147+
req := httptest.NewRequest(http.MethodPost, "/notes/imports", body)
148+
req.Header.Set("Content-Type", "text/plain")
149+
150+
err := ImportNotesValidator(req)
151+
152+
assert.NotNil(t, err)
153+
}

0 commit comments

Comments
 (0)