mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	template: add extension filter test and simplify test code (#1996)
Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
		
							parent
							
								
									8a326d4dc1
								
							
						
					
					
						commit
						55a564df6d
					
				@ -62,8 +62,62 @@ func TestTemplates(t *testing.T) {
 | 
				
			|||||||
		BufPool: &sync.Pool{New: func() interface{} { return new(bytes.Buffer) }},
 | 
							BufPool: &sync.Pool{New: func() interface{} { return new(bytes.Buffer) }},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Test tmpl on /photos/test.html
 | 
						// register custom function which is used in template
 | 
				
			||||||
	req, err := http.NewRequest("GET", "/photos/test.html", nil)
 | 
						httpserver.TemplateFuncs["root"] = func() string { return "root" }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, c := range []struct {
 | 
				
			||||||
 | 
							tpl      Templates
 | 
				
			||||||
 | 
							req      string
 | 
				
			||||||
 | 
							respCode int
 | 
				
			||||||
 | 
							res      string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								tpl:      tmpl,
 | 
				
			||||||
 | 
								req:      "/photos/test.html",
 | 
				
			||||||
 | 
								respCode: http.StatusOK,
 | 
				
			||||||
 | 
								res: `<!DOCTYPE html><html><head><title>test page</title></head><body><h1>Header title</h1>
 | 
				
			||||||
 | 
					</body></html>
 | 
				
			||||||
 | 
					`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								tpl:      tmpl,
 | 
				
			||||||
 | 
								req:      "/images/img.htm",
 | 
				
			||||||
 | 
								respCode: http.StatusOK,
 | 
				
			||||||
 | 
								res: `<!DOCTYPE html><html><head><title>img</title></head><body><h1>Header title</h1>
 | 
				
			||||||
 | 
					</body></html>
 | 
				
			||||||
 | 
					`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								tpl:      tmpl,
 | 
				
			||||||
 | 
								req:      "/images/img2.htm",
 | 
				
			||||||
 | 
								respCode: http.StatusOK,
 | 
				
			||||||
 | 
								res: `<!DOCTYPE html><html><head><title>img</title></head><body>{{.Include "header.html"}}</body></html>
 | 
				
			||||||
 | 
					`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								tpl:      tmplroot,
 | 
				
			||||||
 | 
								req:      "/root.html",
 | 
				
			||||||
 | 
								respCode: http.StatusOK,
 | 
				
			||||||
 | 
								res: `<!DOCTYPE html><html><head><title>root</title></head><body><h1>Header title</h1>
 | 
				
			||||||
 | 
					</body></html>
 | 
				
			||||||
 | 
					`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// test extension filter
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								tpl:      tmplroot,
 | 
				
			||||||
 | 
								req:      "/as_it_is.txt",
 | 
				
			||||||
 | 
								respCode: http.StatusOK,
 | 
				
			||||||
 | 
								res: `<!DOCTYPE html><html><head><title>as it is</title></head><body>{{.Include "header.html"}}</body></html>
 | 
				
			||||||
 | 
					`,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						} {
 | 
				
			||||||
 | 
							c := c
 | 
				
			||||||
 | 
							t.Run("", func(t *testing.T) {
 | 
				
			||||||
 | 
								req, err := http.NewRequest("GET", c.req, nil)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				t.Fatalf("Test: Could not create HTTP request: %v", err)
 | 
									t.Fatalf("Test: Could not create HTTP request: %v", err)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@ -71,91 +125,16 @@ func TestTemplates(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			rec := httptest.NewRecorder()
 | 
								rec := httptest.NewRecorder()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tmpl.ServeHTTP(rec, req)
 | 
								c.tpl.ServeHTTP(rec, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if rec.Code != http.StatusOK {
 | 
								if rec.Code != c.respCode {
 | 
				
			||||||
		t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK)
 | 
									t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, c.respCode)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			respBody := rec.Body.String()
 | 
								respBody := rec.Body.String()
 | 
				
			||||||
	expectedBody := `<!DOCTYPE html><html><head><title>test page</title></head><body><h1>Header title</h1>
 | 
								if respBody != c.res {
 | 
				
			||||||
</body></html>
 | 
									t.Fatalf("Test: the expected body %v is different from the response one: %v", c.res, respBody)
 | 
				
			||||||
`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if respBody != expectedBody {
 | 
					 | 
				
			||||||
		t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody)
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
	// Test tmpl on /images/img.htm
 | 
					 | 
				
			||||||
	req, err = http.NewRequest("GET", "/images/img.htm", nil)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		t.Fatalf("Could not create HTTP request: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	req = req.WithContext(context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	rec = httptest.NewRecorder()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	tmpl.ServeHTTP(rec, req)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if rec.Code != http.StatusOK {
 | 
					 | 
				
			||||||
		t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	respBody = rec.Body.String()
 | 
					 | 
				
			||||||
	expectedBody = `<!DOCTYPE html><html><head><title>img</title></head><body><h1>Header title</h1>
 | 
					 | 
				
			||||||
</body></html>
 | 
					 | 
				
			||||||
`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if respBody != expectedBody {
 | 
					 | 
				
			||||||
		t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Test tmpl on /images/img2.htm
 | 
					 | 
				
			||||||
	req, err = http.NewRequest("GET", "/images/img2.htm", nil)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		t.Fatalf("Could not create HTTP request: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	req = req.WithContext(context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	rec = httptest.NewRecorder()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	tmpl.ServeHTTP(rec, req)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if rec.Code != http.StatusOK {
 | 
					 | 
				
			||||||
		t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	respBody = rec.Body.String()
 | 
					 | 
				
			||||||
	expectedBody = `<!DOCTYPE html><html><head><title>img</title></head><body>{{.Include "header.html"}}</body></html>
 | 
					 | 
				
			||||||
`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if respBody != expectedBody {
 | 
					 | 
				
			||||||
		t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Test tmplroot on /root.html
 | 
					 | 
				
			||||||
	req, err = http.NewRequest("GET", "/root.html", nil)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		t.Fatalf("Could not create HTTP request: %v", err)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	req = req.WithContext(context.WithValue(req.Context(), httpserver.OriginalURLCtxKey, *req.URL))
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	rec = httptest.NewRecorder()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// register custom function which is used in template
 | 
					 | 
				
			||||||
	httpserver.TemplateFuncs["root"] = func() string { return "root" }
 | 
					 | 
				
			||||||
	tmplroot.ServeHTTP(rec, req)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if rec.Code != http.StatusOK {
 | 
					 | 
				
			||||||
		t.Fatalf("Test: Wrong response code: %d, should be %d", rec.Code, http.StatusOK)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	respBody = rec.Body.String()
 | 
					 | 
				
			||||||
	expectedBody = `<!DOCTYPE html><html><head><title>root</title></head><body><h1>Header title</h1>
 | 
					 | 
				
			||||||
</body></html>
 | 
					 | 
				
			||||||
`
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if respBody != expectedBody {
 | 
					 | 
				
			||||||
		t.Fatalf("Test: the expected body %v is different from the response one: %v", expectedBody, respBody)
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										1
									
								
								caddyhttp/templates/testdata/as_it_is.txt
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								caddyhttp/templates/testdata/as_it_is.txt
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					<!DOCTYPE html><html><head><title>as it is</title></head><body>{{.Include "header.html"}}</body></html>
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user