aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMHTTPServerTest.m
blob: d96d54e9282f3e2bb70c9c3caf6b0451113aca4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//
//  GTMHTTPServerTest.m
//
//  Copyright 2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
// 
//  http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import <netinet/in.h>
#import <sys/socket.h>
#import <unistd.h>
#import "GTMSenTestCase.h"
#import "GTMUnitTestDevLog.h"
#import "GTMHTTPServer.h"
#import "GTMRegex.h"

@interface GTMHTTPServerTest : GTMTestCase {
  NSData *fetchedData_;
}
@end

@interface GTMHTTPServerTest (PrivateMethods)
- (NSData *)fetchFromPort:(unsigned short)port
                  payload:(NSString *)payload
                chunkSize:(NSUInteger)chunkSize;
- (NSFileHandle *)fileHandleSendingToPort:(unsigned short)port
                                  payload:(NSString *)payload
                                chunkSize:(NSUInteger)chunkSize;
- (void)readFinished:(NSNotification *)notification;
@end

// helper class
@interface TestServerDelegate : NSObject {
  NSMutableArray *requests_;
  NSMutableArray *responses_;
}
+ (id)testServerDelegate;
- (NSUInteger)requestCount;
- (GTMHTTPRequestMessage *)popRequest;
- (void)pushResponse:(GTMHTTPResponseMessage *)message;
@end

// helper that throws while handling its request
@interface TestThrowingServerDelegate : TestServerDelegate
// since this method ALWAYS throws, we can mark it as noreturn
- (GTMHTTPResponseMessage *)httpServer:(GTMHTTPServer *)server
                         handleRequest:(GTMHTTPRequestMessage *)request __attribute__ ((noreturn));
@end

// The timings used for waiting for replies
const NSTimeInterval kGiveUpInterval = 5.0;
const NSTimeInterval kRunLoopInterval = 0.01;

// the size we break writes up into to test the reading code and how long to
// wait between writes.
const NSUInteger kSendChunkSize = 12;
const NSTimeInterval kSendChunkInterval = 0.05;

// ----------------------------------------------------------------------------

@implementation GTMHTTPServerTest

- (void)testInit {
  // bad delegates
  [GTMUnitTestDevLog expectString:@"missing delegate"];
  STAssertNil([[GTMHTTPServer alloc] init], nil);
  [GTMUnitTestDevLog expectString:@"missing delegate"];
  STAssertNil([[GTMHTTPServer alloc] initWithDelegate:nil], nil);

  TestServerDelegate *delegate = [TestServerDelegate testServerDelegate];
  STAssertNotNil(delegate, nil);
  GTMHTTPServer *server =
    [[[GTMHTTPServer alloc] initWithDelegate:delegate] autorelease];
  STAssertNotNil(server, nil);

  // some attributes

  STAssertTrue([server delegate] == delegate, nil);

  [server setLocalhostOnly:NO];
  STAssertFalse([server localhostOnly], nil);
  [server setLocalhostOnly:YES];
  STAssertTrue([server localhostOnly], nil);
  
  STAssertEquals([server port], (uint16_t)0, nil);
  [server setPort:8080];
  STAssertEquals([server port], (uint16_t)8080, nil);
  [server setPort:80];
  STAssertEquals([server port], (uint16_t)80, nil);
  
  // description (atleast 10 chars)
  STAssertGreaterThan([[server description] length], (NSUInteger)10, nil);
}

- (void)testStartStop {
  TestServerDelegate *delegate1 = [TestServerDelegate testServerDelegate];
  STAssertNotNil(delegate1, nil);
  GTMHTTPServer *server1 =
    [[[GTMHTTPServer alloc] initWithDelegate:delegate1] autorelease];
  STAssertNotNil(server1, nil);
  NSError *error = nil;
  STAssertTrue([server1 start:&error], @"failed to start (error=%@)", error);
  STAssertNil(error, @"error: %@", error);
  STAssertGreaterThanOrEqual([server1 port], (uint16_t)1024,
                             @"how'd we get a reserved port?");

  TestServerDelegate *delegate2 = [TestServerDelegate testServerDelegate];
  STAssertNotNil(delegate2, nil);
  GTMHTTPServer *server2 =
    [[[GTMHTTPServer alloc] initWithDelegate:delegate2] autorelease];
  STAssertNotNil(server2, nil);
  
  // try the reserved port
  [server2 setPort:666];
  error = nil;
  STAssertFalse([server2 start:&error], nil);
  STAssertNotNil(error, nil);
  STAssertEqualObjects([error domain], kGTMHTTPServerErrorDomain, nil);
  STAssertEquals([error code], (NSInteger)kGTMHTTPServerBindFailedError,
                 @"port should have been reserved");
  
  // try the same port
  [server2 setPort:[server1 port]];
  error = nil;
  STAssertFalse([server2 start:&error], nil);
  STAssertNotNil(error, nil);
  STAssertEqualObjects([error domain], kGTMHTTPServerErrorDomain, nil);
  STAssertEquals([error code], (NSInteger)kGTMHTTPServerBindFailedError,
                 @"port should have been in use");
  
  // try a random port again so we really start (prove two can run at once)
  [server2 setPort:0];
  error = nil;
  STAssertTrue([server2 start:&error], @"failed to start (error=%@)", error);
  STAssertNil(error, @"error: %@", error);
  
  // shut them down
  [server1 stop];
  [server2 stop];
}

- (void)testRequests {
  TestServerDelegate *delegate = [TestServerDelegate testServerDelegate];
  STAssertNotNil(delegate, nil);
  GTMHTTPServer *server =
    [[[GTMHTTPServer alloc] initWithDelegate:delegate] autorelease];
  STAssertNotNil(server, nil);
  NSError *error = nil;
  STAssertTrue([server start:&error], @"failed to start (error=%@)", error);
  STAssertNil(error, @"error: %@", error);

  // a request to test all the fields of a request object
  
  NSString *payload =
    @"PUT /some/server/path HTTP/1.0\r\n"
    @"Content-Length: 16\r\n"
    @"Custom-Header: Custom_Value\r\n"
    @"\r\n"
    @"this is the body";
  NSData *reply =
    [self fetchFromPort:[server port] payload:payload chunkSize:kSendChunkSize];
  STAssertNotNil(reply, nil);
  
  GTMHTTPRequestMessage *request = [delegate popRequest];
  STAssertEqualObjects([request version], @"HTTP/1.0", nil);
  STAssertEqualObjects([[request URL] absoluteString], @"/some/server/path", nil);
  STAssertEqualObjects([request method], @"PUT", nil);
  STAssertEqualObjects([request body],
                       [@"this is the body" dataUsingEncoding:NSUTF8StringEncoding],
                       nil);
  NSDictionary *allHeaders = [request allHeaderFieldValues];
  STAssertNotNil(allHeaders, nil);
  STAssertEquals([allHeaders count], (NSUInteger)2, nil);
  STAssertEqualObjects([allHeaders objectForKey:@"Content-Length"],
                       @"16", nil);
  STAssertEqualObjects([allHeaders objectForKey:@"Custom-Header"],
                       @"Custom_Value", nil);
  STAssertGreaterThan([[request description] length], (NSUInteger)10, nil);
  
  // test different request types (in simple form)

  typedef struct  {
    NSString *method;
    NSString *url;
  } TestData;
  
  TestData data[] = {
    { @"GET", @"/foo/bar" },
    { @"HEAD", @"/foo/baz" },
    { @"POST", @"/foo" },
    { @"PUT", @"/foo/spam" },
    { @"DELETE", @"/fooby/doo" },
    { @"TRACE", @"/something.html" },
    { @"CONNECT", @"/spam" },
    { @"OPTIONS", @"/wee/doggies" },
  };

  for (size_t i = 0; i < sizeof(data) / sizeof(TestData); i++) {
    payload = [NSString stringWithFormat:@"%@ %@ HTTP/1.0\r\n\r\n",
               data[i].method, data[i].url];
    STAssertNotNil(payload, nil);
    reply = [self fetchFromPort:[server port] 
                        payload:payload 
                      chunkSize:kSendChunkSize];
    STAssertNotNil(reply, // just want a reply in this test
                   @"failed of method %@", data[i].method);
    request = [delegate popRequest];
    STAssertEqualObjects([[request URL] absoluteString], data[i].url,
                         @"urls didn't match for index %d", i);
    STAssertEqualObjects([request method], data[i].method,
                         @"methods didn't match for index %d", i);
  }
  
  [server stop];
}

- (void)testResponses {

  // some quick init tests for invalid things
  STAssertNil([[GTMHTTPResponseMessage alloc] init], nil);
  STAssertNil([GTMHTTPResponseMessage responseWithBody:nil
                                           contentType:nil
                                            statusCode:99],
              nil);
  STAssertNil([GTMHTTPResponseMessage responseWithBody:nil
                                           contentType:nil
                                            statusCode:602],
              nil);
  
  TestServerDelegate *delegate = [TestServerDelegate testServerDelegate];
  STAssertNotNil(delegate, nil);
  GTMHTTPServer *server =
    [[[GTMHTTPServer alloc] initWithDelegate:delegate] autorelease];
  STAssertNotNil(server, nil);
  NSError *error = nil;
  STAssertTrue([server start:&error], @"failed to start (error=%@)", error);
  STAssertNil(error, @"error: %@", error);

  // test the html helper
  
  GTMHTTPResponseMessage *expectedResponse =
    [GTMHTTPResponseMessage responseWithHTMLString:@"Success!"];
  STAssertNotNil(expectedResponse, nil);
  STAssertGreaterThan([[expectedResponse description] length],
                      (NSUInteger)0, nil);
  [delegate pushResponse:expectedResponse];
  NSData *responseData = [self fetchFromPort:[server port]
                                     payload:@"GET /foo HTTP/1.0\r\n\r\n"
                                   chunkSize:kSendChunkSize];
  STAssertNotNil(responseData, nil);
  NSString *responseString =
    [[[NSString alloc] initWithData:responseData
                           encoding:NSUTF8StringEncoding] autorelease];
  STAssertNotNil(responseString, nil);
  STAssertTrue([responseString hasPrefix:@"HTTP/1.0 200 OK"], nil);
  STAssertTrue([responseString hasSuffix:@"Success!"], @"should end w/ our data");
  STAssertNotEquals([responseString rangeOfString:@"Content-Length: 8"].location,
                    (NSUInteger)NSNotFound, nil);
  STAssertNotEquals([responseString rangeOfString:@"Content-Type: text/html; charset=UTF-8"].location,
                    (NSUInteger)NSNotFound, nil);
  
  // test the plain code response
  
  expectedResponse = [GTMHTTPResponseMessage emptyResponseWithCode:299];
  STAssertNotNil(expectedResponse, nil);
  STAssertGreaterThan([[expectedResponse description] length],
                      (NSUInteger)0, nil);
  [delegate pushResponse:expectedResponse];
  responseData = [self fetchFromPort:[server port]
                             payload:@"GET /foo HTTP/1.0\r\n\r\n"
                           chunkSize:kSendChunkSize];
  STAssertNotNil(responseData, nil);
  responseString =
    [[[NSString alloc] initWithData:responseData
                           encoding:NSUTF8StringEncoding] autorelease];
  STAssertNotNil(responseString, nil);
  STAssertTrue([responseString hasPrefix:@"HTTP/1.0 299 "], nil);
  STAssertNotEquals([responseString rangeOfString:@"Content-Length: 0"].location,
                    (NSUInteger)NSNotFound, nil);
  STAssertNotEquals([responseString rangeOfString:@"Content-Type: text/html"].location,
                    (NSUInteger)NSNotFound, nil);

  // test the general api w/ extra header add

  expectedResponse =
    [GTMHTTPResponseMessage responseWithBody:[@"FOO" dataUsingEncoding:NSUTF8StringEncoding]
                                 contentType:@"some/type"
                                  statusCode:298];
  STAssertNotNil(expectedResponse, nil);
  STAssertGreaterThan([[expectedResponse description] length],
                      (NSUInteger)0, nil);
  [expectedResponse setValue:@"Custom_Value"
              forHeaderField:@"Custom-Header"];
  [expectedResponse setValue:nil
              forHeaderField:@"Custom-Header2"];
  [delegate pushResponse:expectedResponse];
  responseData = [self fetchFromPort:[server port]
                             payload:@"GET /foo HTTP/1.0\r\n\r\n"
                           chunkSize:kSendChunkSize];
  STAssertNotNil(responseData, nil);
  responseString =
    [[[NSString alloc] initWithData:responseData
                           encoding:NSUTF8StringEncoding] autorelease];
  STAssertNotNil(responseString, nil);
  STAssertTrue([responseString hasPrefix:@"HTTP/1.0 298"], nil);
  STAssertTrue([responseString hasSuffix:@"FOO"], @"should end w/ our data");
  STAssertNotEquals([responseString rangeOfString:@"Content-Length: 3"].location,
                    (NSUInteger)NSNotFound, nil);
  STAssertNotEquals([responseString rangeOfString:@"Content-Type: some/type"].location,
                    (NSUInteger)NSNotFound, nil);
  STAssertNotEquals([responseString rangeOfString:@"Custom-Header: Custom_Value"].location,
                    (NSUInteger)NSNotFound, nil);
  STAssertNotEquals([responseString rangeOfString:@"Custom-Header2: "].location,
                    (NSUInteger)NSNotFound, nil);
  
  [server stop];
}

- (void)testRequstEdgeCases {
  // test all the odd things about requests

  TestServerDelegate *delegate = [TestServerDelegate testServerDelegate];
  STAssertNotNil(delegate, nil);
  GTMHTTPServer *server =
    [[[GTMHTTPServer alloc] initWithDelegate:delegate] autorelease];
  STAssertNotNil(server, nil);
  NSError *error = nil;
  STAssertTrue([server start:&error], @"failed to start (error=%@)", error);
  STAssertNil(error, @"error: %@", error);
  
  // extra data (ie-pipelining)

  NSString *payload =
    @"GET /some/server/path HTTP/1.0\r\n"
    @"\r\n"
    @"GET /some/server/path/too HTTP/1.0\r\n"
    @"\r\n";
  // don't chunk this, we want to make sure both requests get to our server
  [GTMUnitTestDevLog expectString:@"Got 38 extra bytes on http request, "
                                    "ignoring them"];
  NSData *reply =
    [self fetchFromPort:[server port] payload:payload chunkSize:0];
  STAssertNotNil(reply, nil);
  STAssertEquals([delegate requestCount], (NSUInteger)1, nil);
  
  // close w/o full request
  {
    // local pool so we can force our handle to close
    NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init];
    NSFileHandle *handle =
      [self fileHandleSendingToPort:[server port]
                            payload:@"GET /some/server/path HTTP/"
                          chunkSize:kSendChunkSize];
    STAssertNotNil(handle, nil);
    // spin the run loop so reads the start of the request
    NSDate* loopIntervalDate =
      [NSDate dateWithTimeIntervalSinceNow:kRunLoopInterval];
    [[NSRunLoop currentRunLoop] runUntilDate:loopIntervalDate]; 
    // make sure we see the request at this point
    STAssertEquals([server activeRequestCount], (NSUInteger)1,
                   @"should have started the request by now");
    // drop the pool to close the connection
    [localPool release];
    // spin the run loop so it should see the close
    loopIntervalDate = [NSDate dateWithTimeIntervalSinceNow:kRunLoopInterval];
    [[NSRunLoop currentRunLoop] runUntilDate:loopIntervalDate];
    // make sure we didn't get a request (1 is from test before) and make sure
    // we don't have some in flight.
    STAssertEquals([delegate requestCount], (NSUInteger)1,
                   @"shouldn't have gotten another request");
    STAssertEquals([server activeRequestCount], (NSUInteger)0,
                   @"should have cleaned up the pending connection");
  }
  
}

- (void)testExceptionDuringRequest {

  TestServerDelegate *delegate = [TestThrowingServerDelegate testServerDelegate];
  STAssertNotNil(delegate, nil);
  GTMHTTPServer *server =
    [[[GTMHTTPServer alloc] initWithDelegate:delegate] autorelease];
  STAssertNotNil(server, nil);
  NSError *error = nil;
  STAssertTrue([server start:&error], @"failed to start (error=%@)", error);
  STAssertNil(error, @"error: %@", error);
  [GTMUnitTestDevLog expectString:@"Exception trying to handle http request: "
   "To test our handling"];
  NSData *responseData = [self fetchFromPort:[server port]
                                     payload:@"GET /foo HTTP/1.0\r\n\r\n"
                                   chunkSize:kSendChunkSize];
  STAssertNotNil(responseData, nil);
  STAssertEquals([responseData length], (NSUInteger)0, nil);
  STAssertEquals([delegate requestCount], (NSUInteger)1, nil);
  STAssertEquals([server activeRequestCount], (NSUInteger)0, nil);
}

@end

// ----------------------------------------------------------------------------

@implementation GTMHTTPServerTest (PrivateMethods)

- (NSData *)fetchFromPort:(unsigned short)port
                  payload:(NSString *)payload
                chunkSize:(NSUInteger)chunkSize {
  fetchedData_ = nil;

  NSFileHandle *handle = [self fileHandleSendingToPort:port
                                               payload:payload
                                             chunkSize:chunkSize];

  NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  [center addObserver:self
             selector:@selector(readFinished:)
                 name:NSFileHandleReadToEndOfFileCompletionNotification
               object:handle];
  [handle readToEndOfFileInBackgroundAndNotify];

  // wait for our reply
  NSDate* giveUpDate = [NSDate dateWithTimeIntervalSinceNow:kGiveUpInterval];
  while (!fetchedData_ && [giveUpDate timeIntervalSinceNow] > 0) {
    NSDate* loopIntervalDate =
      [NSDate dateWithTimeIntervalSinceNow:kRunLoopInterval];
    [[NSRunLoop currentRunLoop] runUntilDate:loopIntervalDate]; 
  }

  [center removeObserver:self
                 name:NSFileHandleReadToEndOfFileCompletionNotification
               object:handle];
  
  NSData *result = [fetchedData_ autorelease];
  fetchedData_ = nil;
  return result;
}

- (NSFileHandle *)fileHandleSendingToPort:(unsigned short)port
                                  payload:(NSString *)payload
                                chunkSize:(NSUInteger)chunkSize {
  int fd = socket(AF_INET, SOCK_STREAM, 0);
  STAssertGreaterThan(fd, 0, @"failed to create socket");
  
  struct sockaddr_in addr;
  bzero(&addr, sizeof(addr));
  addr.sin_len    = sizeof(addr);
  addr.sin_family = AF_INET;
  addr.sin_port   = htons(port);
  addr.sin_addr.s_addr = htonl(0x7F000001);
  int connectResult =
  connect(fd, (struct sockaddr*)(&addr), (socklen_t)sizeof(addr));
  STAssertEquals(connectResult, 0, nil);
  
  NSFileHandle *handle =
  [[[NSFileHandle alloc] initWithFileDescriptor:fd
                                 closeOnDealloc:YES] autorelease];
  STAssertNotNil(handle, nil);
  
  NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];
  
  // we can send in one block or in chunked mode
  if (chunkSize > 0) {
    // we don't write the data in one large block, instead of write it out
    // in bits to help test the data collection code.
    NSUInteger length = [payloadData length];
    for (NSUInteger x = 0 ; x < length ; x += chunkSize) {
      NSUInteger dataChunkSize = length - x;
      if (dataChunkSize > chunkSize) {
        dataChunkSize = chunkSize;
      }
      NSData *dataChunk 
        = [payloadData subdataWithRange:NSMakeRange(x, dataChunkSize)];
      [handle writeData:dataChunk];
      // delay after all but the last chunk to give it time to be read.
      if ((x + chunkSize) < length) {
        NSDate* loopIntervalDate =
          [NSDate dateWithTimeIntervalSinceNow:kSendChunkInterval];
        [[NSRunLoop currentRunLoop] runUntilDate:loopIntervalDate]; 
      }
    }
  } else {
    [handle writeData:payloadData];
  }
  
  return handle;
}

- (void)readFinished:(NSNotification *)notification {
  NSDictionary *userInfo = [notification userInfo];
  fetchedData_ =
    [[userInfo objectForKey:NSFileHandleNotificationDataItem] retain];
}

@end

// ----------------------------------------------------------------------------

@implementation TestServerDelegate

- (id)init {
  self = [super init];
  if (self) {
    requests_ = [[NSMutableArray alloc] init];
    responses_ = [[NSMutableArray alloc] init];
  }
  return self;
}

- (void)dealloc {
  [requests_ release];
  [responses_ release];
  [super dealloc];
}

+ (id)testServerDelegate {
  return [[[[self class] alloc] init] autorelease];
}

- (NSUInteger)requestCount {
  return [requests_ count];
}

- (GTMHTTPRequestMessage *)popRequest {
  GTMHTTPRequestMessage *result = [[[requests_ lastObject] retain] autorelease];
  [requests_ removeLastObject];
  return result;
}

- (void)pushResponse:(GTMHTTPResponseMessage *)message {
  [responses_ addObject:message];
}

- (GTMHTTPResponseMessage *)httpServer:(GTMHTTPServer *)server
                         handleRequest:(GTMHTTPRequestMessage *)request {
  [requests_ addObject:request];
  
  GTMHTTPResponseMessage *result = nil;
  if ([responses_ count] > 0) {
    result = [[[responses_ lastObject] retain] autorelease];
    [responses_ removeLastObject];
  } else {
    result = [GTMHTTPResponseMessage responseWithHTMLString:@"success"];
  }
  return result;
}

@end

// ----------------------------------------------------------------------------

@implementation TestThrowingServerDelegate

- (GTMHTTPResponseMessage *)httpServer:(GTMHTTPServer *)server
                         handleRequest:(GTMHTTPRequestMessage *)request {
  // let the base do its normal work for counts, etc.
  [super httpServer:server handleRequest:request];
  NSException *exception =
    [NSException exceptionWithName:@"InternalTestingException"
                            reason:@"To test our handling"
                          userInfo:nil];
  @throw exception;
}

@end