private void assertViaHeaders(final String... vias) throws Exception {
    final String[] formatedViaValues = new String[vias.length];
    for (int i = 0; i < vias.length; ++i) {
      formatedViaValues[i] = String.format(vias[i], i);
      if (formatedViaValues[i].startsWith("Via: ")) {
        formatedViaValues[i] = formatedViaValues[i].substring("Via: ".length());
      }
    }

    SipResponse response = createResponseWithViaHeaders(vias);

    // this should always return the top-most via header which will
    // always have the branchBase-0 as its branch since that is how we generate
    // the branches...
    final ViaHeader via = response.getViaHeader();
    assertThat(via.getBranch().toString(), is(this.branchBase + 0));
    assertThat(via.getValue().toString(), is(formatedViaValues[0]));

    // make sure that the other Via-header is still in the msg
    assertViasInMessageDump(response, formatedViaValues);

    // do it again but ask for all the Via headers up front since
    // the parsing may be slightly different (which it is)
    response = createResponseWithViaHeaders(vias);
    final List<ViaHeader> viaHeaders = response.getViaHeaders();
    assertThat(viaHeaders.size(), is(vias.length));
    for (int i = 0; i < viaHeaders.size(); ++i) {
      assertThat(viaHeaders.get(i).getBranch().toString().contains(this.branchBase + i), is(true));
    }
  }
 private void assertTopMostVia(
     final SipMessage msg, final String host, final int port, final String transport)
     throws Exception {
   final ViaHeader via =
       this.headerFactory.createViaHeader(Buffers.wrap(host), port, Buffers.wrap(transport), null);
   msg.addHeaderFirst(via);
   final ViaHeader topMostVia = msg.getViaHeader();
   assertThat(topMostVia.getBranch(), not((Buffer) null));
   assertThat(topMostVia.getHost().toString(), is(host));
   assertThat(topMostVia.getPort(), is(port));
   assertThat(topMostVia.getTransport().toString(), is(transport));
 }
  /** Make sure that all our Via-headers can be modified the way we need it. */
  @Test
  public void testViaHeaderManipulation() throws Exception {
    final SipRequest request = (SipRequest) parseMessage(RawData.sipInvite);
    final ViaHeader topMostVia = request.getViaHeader();
    assertThat(topMostVia.getBranch().toString(), is("z9hG4bK-16732-1-0"));
    assertThat(topMostVia.getHost().toString(), is("127.0.1.1"));
    assertThat(topMostVia.getPort(), is(5060));

    assertTopMostVia(request, "192.168.0.100", 6789, "TCP");
    assertTopMostVia(request, "192.168.0.101", 1111, "UDP");
    final String str = request.toString();
    assertThat(str.contains("Via: SIP/2.0/TCP 192.168.0.100:6789"), is(true));
    assertThat(str.contains("Via: SIP/2.0/UDP 192.168.0.101:1111"), is(true));
  }
Exemple #4
0
  @Override
  public ViaHeader popViaHeader() throws SipParseException {
    final SipHeader header = popHeader(ViaHeader.NAME);
    if (header instanceof ViaHeader) {
      return (ViaHeader) header;
    }

    if (header == null) {
      return null;
    }

    final Buffer buffer = header.getValue();
    return ViaHeader.frame(buffer);
  }