response_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2011 Andy Balholm. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package icap
  5. import (
  6. "io"
  7. "io/ioutil"
  8. "net"
  9. "testing"
  10. )
  11. const serverAddr = "localhost:11344"
  12. // REQMOD example 2 from RFC 3507, adjusted for order of headers, etc.
  13. func TestREQMOD2(t *testing.T) {
  14. request :=
  15. "REQMOD icap://icap-server.net/server?arg=87 ICAP/1.0\r\n" +
  16. "Host: icap-server.net\r\n" +
  17. "Encapsulated: req-hdr=0, req-body=154\r\n" +
  18. "\r\n" +
  19. "POST /origin-resource/form.pl HTTP/1.1\r\n" +
  20. "Host: www.origin-server.com\r\n" +
  21. "Accept: text/html, text/plain\r\n" +
  22. "Accept-Encoding: compress\r\n" +
  23. "Cache-Control: no-cache\r\n" +
  24. "\r\n" +
  25. "1e\r\n" +
  26. "I am posting this information.\r\n" +
  27. "0\r\n" +
  28. "\r\n"
  29. resp :=
  30. "ICAP/1.0 200 OK\r\n" +
  31. "Connection: close\r\n" +
  32. "Date: Mon, 10 Jan 2000 09:55:21 GMT\r\n" +
  33. "Encapsulated: req-hdr=0, req-body=231\r\n" +
  34. "Istag: \"W3E4R7U9-L2E4-2\"\r\n" +
  35. "Server: ICAP-Server-Software/1.0\r\n" +
  36. "\r\n" +
  37. "POST /origin-resource/form.pl HTTP/1.1\r\n" +
  38. "Accept: text/html, text/plain, image/gif\r\n" +
  39. "Accept-Encoding: gzip, compress\r\n" +
  40. "Cache-Control: no-cache\r\n" +
  41. "Host: www.origin-server.com\r\n" +
  42. "Via: 1.0 icap-server.net (ICAP Example ReqMod Service 1.1)\r\n" +
  43. "\r\n" +
  44. "2d\r\n" +
  45. "I am posting this information. ICAP powered!\r\n" +
  46. "0\r\n" +
  47. "\r\n"
  48. HandleFunc("/server", HandleREQMOD2)
  49. go ListenAndServe(serverAddr, nil)
  50. conn, err := net.Dial("tcp", serverAddr)
  51. if err != nil {
  52. t.Fatalf("could not connect to ICAP server on localhost")
  53. }
  54. io.WriteString(conn, request)
  55. respBuffer := make([]byte, len(resp))
  56. _, err = io.ReadFull(conn, respBuffer)
  57. if err != nil {
  58. t.Fatalf("error while reading response: %v", err)
  59. }
  60. response := string(respBuffer)
  61. checkString("Response", response, resp, t)
  62. }
  63. func HandleREQMOD2(w ResponseWriter, req *Request) {
  64. w.Header().Set("Date", "Mon, 10 Jan 2000 09:55:21 GMT")
  65. w.Header().Set("Server", "ICAP-Server-Software/1.0")
  66. w.Header().Set("ISTag", "\"W3E4R7U9-L2E4-2\"")
  67. req.Request.Header.Set("Via", "1.0 icap-server.net (ICAP Example ReqMod Service 1.1)")
  68. req.Request.Header.Set("Accept", "text/html, text/plain, image/gif")
  69. req.Request.Header.Set("Accept-Encoding", "gzip, compress")
  70. body, _ := ioutil.ReadAll(req.Request.Body)
  71. newBody := string(body) + " ICAP powered!"
  72. w.WriteHeader(200, req.Request, true)
  73. io.WriteString(w, newBody)
  74. }