mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	Merge pull request #165 from Karthic-Hackintosh/master
Test for webSocketParse function in config/setup
This commit is contained in:
		
						commit
						640cd059ce
					
				@ -8,7 +8,7 @@ import (
 | 
				
			|||||||
// WebSocket configures a new WebSockets middleware instance.
 | 
					// WebSocket configures a new WebSockets middleware instance.
 | 
				
			||||||
func WebSocket(c *Controller) (middleware.Middleware, error) {
 | 
					func WebSocket(c *Controller) (middleware.Middleware, error) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	websocks, err := websocketParse(c)
 | 
						websocks, err := webSocketParse(c)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -20,7 +20,7 @@ func WebSocket(c *Controller) (middleware.Middleware, error) {
 | 
				
			|||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func websocketParse(c *Controller) ([]websockets.Config, error) {
 | 
					func webSocketParse(c *Controller) ([]websockets.Config, error) {
 | 
				
			||||||
	var websocks []websockets.Config
 | 
						var websocks []websockets.Config
 | 
				
			||||||
	var respawn bool
 | 
						var respawn bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -34,3 +34,52 @@ func TestWebSocket(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					func TestWebSocketParse(t *testing.T) {
 | 
				
			||||||
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							inputWebSocketConfig    string
 | 
				
			||||||
 | 
							shouldErr               bool
 | 
				
			||||||
 | 
							expectedWebSocketConfig []websockets.Config
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{`websocket /api1 cat`, false, []websockets.Config{{
 | 
				
			||||||
 | 
								Path:    "/api1",
 | 
				
			||||||
 | 
								Command: "cat",
 | 
				
			||||||
 | 
							}}},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							{`websocket /api3 cat  
 | 
				
			||||||
 | 
							  websocket /api4 cat `, false, []websockets.Config{{
 | 
				
			||||||
 | 
								Path:    "/api3",
 | 
				
			||||||
 | 
								Command: "cat",
 | 
				
			||||||
 | 
							}, {
 | 
				
			||||||
 | 
								Path:    "/api4",
 | 
				
			||||||
 | 
								Command: "cat",
 | 
				
			||||||
 | 
							}}},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for i, test := range tests {
 | 
				
			||||||
 | 
							c := NewTestController(test.inputWebSocketConfig)
 | 
				
			||||||
 | 
							actualWebSocketConfigs, err := webSocketParse(c)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if err == nil && test.shouldErr {
 | 
				
			||||||
 | 
								t.Errorf("Test %d didn't error, but it should have", i)
 | 
				
			||||||
 | 
							} else if err != nil && !test.shouldErr {
 | 
				
			||||||
 | 
								t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							if len(actualWebSocketConfigs) != len(test.expectedWebSocketConfig) {
 | 
				
			||||||
 | 
								t.Fatalf("Test %d expected %d no of WebSocket configs, but got %d ",
 | 
				
			||||||
 | 
									i, len(test.expectedWebSocketConfig), len(actualWebSocketConfigs))
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for j, actualWebSocketConfig := range actualWebSocketConfigs {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if actualWebSocketConfig.Path != test.expectedWebSocketConfig[j].Path {
 | 
				
			||||||
 | 
									t.Errorf("Test %d expected %dth WebSocket Config Path to be  %s  , but got %s",
 | 
				
			||||||
 | 
										i, j, test.expectedWebSocketConfig[j].Path, actualWebSocketConfig.Path)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if actualWebSocketConfig.Command != test.expectedWebSocketConfig[j].Command {
 | 
				
			||||||
 | 
									t.Errorf("Test %d expected %dth WebSocket Config Command to be  %s  , but got %s",
 | 
				
			||||||
 | 
										i, j, test.expectedWebSocketConfig[j].Command, actualWebSocketConfig.Command)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user