@@ -77,3 +77,56 @@ func TestDetectUnknown(t *testing.T) {
7777 t .Errorf (`Wrong format detected: %q instead of %q` , format , FormatUnknown )
7878 }
7979}
80+
81+ func TestDetectJSONWithLargeLeadingWhitespace (t * testing.T ) {
82+ leadingWhitespace := strings .Repeat (" " , 10000 )
83+ data := leadingWhitespace + `{
84+ "version" : "https://jsonfeed.org/version/1",
85+ "title" : "Example with lots of leading whitespace"
86+ }`
87+ format , _ := DetectFeedFormat (strings .NewReader (data ))
88+
89+ if format != FormatJSON {
90+ t .Errorf (`Wrong format detected: %q instead of %q` , format , FormatJSON )
91+ }
92+ }
93+
94+ func TestDetectJSONWithMixedWhitespace (t * testing.T ) {
95+ leadingWhitespace := strings .Repeat ("\n \t " , 10000 )
96+ data := leadingWhitespace + `{
97+ "version" : "https://jsonfeed.org/version/1",
98+ "title" : "Example with mixed whitespace"
99+ }`
100+ format , _ := DetectFeedFormat (strings .NewReader (data ))
101+
102+ if format != FormatJSON {
103+ t .Errorf (`Wrong format detected: %q instead of %q` , format , FormatJSON )
104+ }
105+ }
106+
107+ func TestDetectOnlyWhitespace (t * testing.T ) {
108+ data := strings .Repeat (" \t \n \r " , 10000 )
109+ format , _ := DetectFeedFormat (strings .NewReader (data ))
110+
111+ if format != FormatUnknown {
112+ t .Errorf (`Wrong format detected: %q instead of %q` , format , FormatUnknown )
113+ }
114+ }
115+
116+ func TestDetectJSONSmallerThanBuffer (t * testing.T ) {
117+ data := `{"version":"1"}` // This is only 15 bytes, well below the 32-byte buffer
118+ format , _ := DetectFeedFormat (strings .NewReader (data ))
119+
120+ if format != FormatJSON {
121+ t .Errorf (`Wrong format detected: %q instead of %q` , format , FormatJSON )
122+ }
123+ }
124+
125+ func TestDetectJSONWithWhitespaceSmallerThanBuffer (t * testing.T ) {
126+ data := ` {"title":"test"} `
127+ format , _ := DetectFeedFormat (strings .NewReader (data ))
128+
129+ if format != FormatJSON {
130+ t .Errorf (`Wrong format detected: %q instead of %q` , format , FormatJSON )
131+ }
132+ }
0 commit comments