upnp_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package upnp
  7. import (
  8. "encoding/xml"
  9. "net/url"
  10. "testing"
  11. )
  12. func TestExternalIPParsing(t *testing.T) {
  13. soapResponse :=
  14. []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  15. <s:Body>
  16. <u:GetExternalIPAddressResponse xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1">
  17. <NewExternalIPAddress>1.2.3.4</NewExternalIPAddress>
  18. </u:GetExternalIPAddressResponse>
  19. </s:Body>
  20. </s:Envelope>`)
  21. envelope := &soapGetExternalIPAddressResponseEnvelope{}
  22. err := xml.Unmarshal(soapResponse, envelope)
  23. if err != nil {
  24. t.Error(err)
  25. }
  26. if envelope.Body.GetExternalIPAddressResponse.NewExternalIPAddress != "1.2.3.4" {
  27. t.Error("Parse of SOAP request failed.")
  28. }
  29. }
  30. func TestControlURLParsing(t *testing.T) {
  31. rootURL := "http://192.168.243.1:80/igd.xml"
  32. u, _ := url.Parse(rootURL)
  33. subject := "/upnp?control=WANCommonIFC1"
  34. expected := "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  35. replaceRawPath(u, subject)
  36. if u.String() != expected {
  37. t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
  38. }
  39. u, _ = url.Parse(rootURL)
  40. subject = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  41. expected = "http://192.168.243.1:80/upnp?control=WANCommonIFC1"
  42. replaceRawPath(u, subject)
  43. if u.String() != expected {
  44. t.Error("URL normalization of", subject, "failed; expected", expected, "got", u.String())
  45. }
  46. }