Index: trunk/server_test.go
===================================================================
--- trunk/server_test.go	(revision 601)
+++ trunk/server_test.go	(revision 602)
@@ -9,4 +9,6 @@
 )
 
+var testServerPrefix = &irc.Prefix{Name: "soju-test-server"}
+
 const (
 	testUsername = "soju-test-user"
@@ -14,7 +16,8 @@
 )
 
-func ircPipe() (ircConn, ircConn) {
+func createTestDownstream(t *testing.T, srv *Server) ircConn {
 	c1, c2 := net.Pipe()
-	return newNetIRCConn(c1), newNetIRCConn(c2)
+	go srv.handle(newNetIRCConn(c1))
+	return newNetIRCConn(c2)
 }
 
@@ -31,7 +34,5 @@
 }
 
-func createTestUser(t *testing.T) *Server {
-	db := createTempDB(t)
-
+func createTestUser(t *testing.T, db Database) *User {
 	hashed, err := bcrypt.GenerateFromPassword([]byte(testPassword), bcrypt.DefaultCost)
 	if err != nil {
@@ -44,5 +45,48 @@
 	}
 
-	return NewServer(db)
+	return record
+}
+
+type testUpstream struct {
+	net.Listener
+	Accept chan ircConn
+}
+
+func createTestUpstream(t *testing.T, db Database, user *User) (*Network, *testUpstream) {
+	ln, err := net.Listen("tcp", "localhost:0")
+	if err != nil {
+		t.Fatalf("failed to create TCP listener: %v", err)
+	}
+
+	tu := &testUpstream{
+		Listener: ln,
+		Accept:   make(chan ircConn),
+	}
+
+	go func() {
+		defer close(tu.Accept)
+
+		for {
+			c, err := ln.Accept()
+			if isErrClosed(err) {
+				break
+			} else if err != nil {
+				t.Fatalf("failed accepting connection: %v", err)
+			}
+			tu.Accept <- newNetIRCConn(c)
+		}
+	}()
+
+	network := &Network{
+		Name:    "testnet",
+		Addr:    "irc+insecure://" + ln.Addr().String(),
+		Nick:    user.Username,
+		Enabled: true,
+	}
+	if err := db.StoreNetwork(user.ID, network); err != nil {
+		t.Fatalf("failed to store test network: %v", err)
+	}
+
+	return network, tu
 }
 
@@ -58,5 +102,5 @@
 }
 
-func authTestUser(t *testing.T, c ircConn) {
+func registerDownstreamConn(t *testing.T, c ircConn, network *Network) {
 	c.WriteMessage(&irc.Message{
 		Command: "PASS",
@@ -69,5 +113,5 @@
 	c.WriteMessage(&irc.Message{
 		Command: "USER",
-		Params:  []string{testUsername, "0", "*", testUsername},
+		Params:  []string{testUsername + "/" + network.Name, "0", "*", testUsername},
 	})
 
@@ -75,6 +119,50 @@
 }
 
+func registerUpstreamConn(t *testing.T, c ircConn) {
+	msg := expectMessage(t, c, "CAP")
+	if msg.Params[0] != "LS" {
+		t.Fatalf("invalid CAP LS: got: %v", msg)
+	}
+	msg = expectMessage(t, c, "NICK")
+	nick := msg.Params[0]
+	if nick != testUsername {
+		t.Fatalf("invalid NICK: want %q, got: %v", testUsername, msg)
+	}
+	expectMessage(t, c, "USER")
+
+	c.WriteMessage(&irc.Message{
+		Prefix:  testServerPrefix,
+		Command: irc.RPL_WELCOME,
+		Params:  []string{nick, "Welcome!"},
+	})
+	c.WriteMessage(&irc.Message{
+		Prefix:  testServerPrefix,
+		Command: irc.RPL_YOURHOST,
+		Params:  []string{nick, "Your host is soju-test-server"},
+	})
+	c.WriteMessage(&irc.Message{
+		Prefix:  testServerPrefix,
+		Command: irc.RPL_CREATED,
+		Params:  []string{nick, "Who cares when the server was created?"},
+	})
+	c.WriteMessage(&irc.Message{
+		Prefix:  testServerPrefix,
+		Command: irc.RPL_MYINFO,
+		Params:  []string{nick, testServerPrefix.Name, "soju", "aiwroO", "OovaimnqpsrtklbeI"},
+	})
+	c.WriteMessage(&irc.Message{
+		Prefix:  testServerPrefix,
+		Command: irc.ERR_NOMOTD,
+		Params:  []string{nick, "No MOTD"},
+	})
+}
+
 func TestServer(t *testing.T) {
-	srv := createTestUser(t)
+	db := createTempDB(t)
+	user := createTestUser(t, db)
+	network, upstream := createTestUpstream(t, db, user)
+	defer upstream.Close()
+
+	srv := NewServer(db)
 	if err := srv.Start(); err != nil {
 		t.Fatalf("failed to start server: %v", err)
@@ -82,8 +170,11 @@
 	defer srv.Shutdown()
 
-	c, srvConn := ircPipe()
-	defer c.Close()
-	go srv.handle(srvConn)
+	uc := <-upstream.Accept
+	defer uc.Close()
+	registerUpstreamConn(t, uc)
 
-	authTestUser(t, c)
+	dc := createTestDownstream(t, srv)
+	defer dc.Close()
+
+	registerDownstreamConn(t, dc, network)
 }
