<commit_before>#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
#import "SpecHelper.h"
#else
#import <Cedar/SpecHelper.h>
#endif

#import "NSURLConnection+Spec.h"
#import "PSHKFakeHTTPURLResponse.h"
#import "NSURLConnectionDelegate.h"
#import "ConnectionDelegate.h"
#import "FakeHTTP.h"
#import "FakeOperationQueue.h"


@interface SelfReferentialConnection : NSURLConnection
@end

@implementation SelfReferentialConnection

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately {
    return [super initWithRequest:request delegate:self startImmediately:startImmediately];
}

@end


using namespace Cedar::Matchers;
using namespace Cedar::Doubles;


SPEC_BEGIN(NSURLConnectionSpec_Spec)

describe(@"NSURLConnection (spec extensions)", ^{
    __block NSURLRequest *request;
    __block NSURLConnection *connection;
    __block id<NSURLConnectionDelegate, CedarDouble> delegate;

    beforeEach(^{
        delegate = nice_fake_for(@protocol(NSURLConnectionDataDelegate));
        NSURL *url = [NSURL URLWithString:@"http://example.com"];
        request = [NSURLRequest requestWithURL:url];

        // OS X 10.7 introduced the NSURLDownload class, which has an initWithRequest:delegate: initializer
        // exactly like the one on NSURLConnection, EXCEPT that the type of the delegete argument is
        // id<NSURLDownloadDelegate> while the type of the NSURLConnection delegate argument is id.  The
        // compiler sees these as ambiguous methods with different arguments, so emits a warning.  Explicitly
        // cast the result of the alloc to NSURLConnection * to quiet the compiler.
        connection = [[(NSURLConnection *)[NSURLConnection alloc] initWithRequest:request delegate:delegate] autorelease];
    });

    describe(@"+resetAll", ^{
        it(@"should remove all connections from the global list of connections", ^{
            expect([NSURLConnection connections]).to_not(be_empty());
            [NSURLConnection resetAll];
            expect([NSURLConnection connections]).to(be_empty());
        });
    });

    describe(@"on initialization", ^{
        it(@"should record the existence of the object in the global list of connections", ^{
            expect([NSURLConnection connections]).to(contain(connection));
        });

        it(@"should retain the request", ^{
            expect(request.retainCount).to(equal(2));
        });

        describe(@"when the delegate is self", ^{
            beforeEach(^{
                connection = [[[SelfReferentialConnection alloc] initWithRequest:request delegate:nil startImmediately:YES] autorelease];
            });

            it(@"should not retain the delegate", ^{
                // One retain for alloc, one retain for the connections array.
                expect(connection.retainCount).to(equal(2));
            });
        });

        describe(@"when the delegate is not self", ^{
            __block ConnectionDelegate *delegate;

            beforeEach(^{
                delegate = [[[ConnectionDelegate alloc] init] autorelease];
                connection = [[(NSURLConnection *)[NSURLConnection alloc] initWithRequest:request delegate:delegate] autorelease];
            });

            it(@"should retain the delegate", ^{
                expect([delegate retainCount]).to(equal(2));
            });
        });
    });

    describe(@"on destruction", ^{
        beforeEach(^{
            request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
            delegate = nice_fake_for(@protocol(NSURLConnectionDataDelegate));

            connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
            [connection release]; connection = nil;
            [NSURLConnection resetAll];
        });

        it(@"should release the request", ^{
            expect(request.retainCount).to(equal(1));
        });

        describe(@"when the delegate is self", ^{
            beforeEach(^{
                connection = [[SelfReferentialConnection alloc] initWithRequest:request delegate:nil startImmediately:YES];
                [NSURLConnection resetAll];
            });

            it(@"should not explode", ^{
                [connection release]; connection = nil;
            });
        });

        describe(@"when the delegate is not self", ^{
            it(@"should release the delegate", ^{
                expect([delegate retainCount]).to(equal(1));
            });
        });
    });

    describe(@"when the connection is generated with the asynchronous convenience class method", ^{
        __block NSURL *URL;
        __block NSHTTPURLResponse *receivedResponse;
        __block NSData *receivedData;
        __block NSError *receivedError;

        beforeEach(^{
            receivedResponse = nil;
            receivedData = nil;
            receivedError = nil;

            URL = [NSURL URLWithString:@"http://www.google.com/"];
            NSURLRequest *request = [NSURLRequest requestWithURL:URL];
            [NSURLConnection sendAsynchronousRequest:request
                                               queue:[NSOperationQueue mainQueue]
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                       receivedData = [data retain];
                                       receivedError = [error retain];
                                       receivedResponse = [(NSHTTPURLResponse *)response retain];
                                   }];
            connection = [[NSURLConnection connections] lastObject];
        });

        afterEach(^{
            [receivedData release];
            [receivedError release];
            [receivedResponse release];
        });

        it(@"should be captured and made available in the array of connections", ^{
            connection.request.URL should equal(URL);
        });

        it(@"should receive responses", ^{
            PSHKFakeHTTPURLResponse *response = [[[PSHKFakeHTTPURLResponse alloc] initWithStatusCode:200 andHeaders:nil andBody:@"Response"] autorelease];

            [connection receiveResponse:response];

            NSString *receivedString = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];
            receivedString should equal(@"Response");
            receivedResponse.statusCode should equal(200);
        });

        it(@"should receive failures", ^{
            NSData *data = [@"Fail" dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = [[[NSError alloc] init] autorelease];

            [connection failWithError:error data:data];

            receivedData should be_nil; //SDK docs say that data is guaranteed to be nil when an error occurs
            receivedError should equal(error);
        });
    });<commit_msg>Removed exploratory check to fix exploding specs.<commit_after>#import <Foundation/Foundation.h>
#if TARGET_OS_IPHONE
#import "SpecHelper.h"
#else
#import <Cedar/SpecHelper.h>
#endif

#import "NSURLConnection+Spec.h"
#import "PSHKFakeHTTPURLResponse.h"
#import "NSURLConnectionDelegate.h"
#import "ConnectionDelegate.h"
#import "FakeHTTP.h"
#import "FakeOperationQueue.h"


@interface SelfReferentialConnection : NSURLConnection
@end

@implementation SelfReferentialConnection

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately {
    return [super initWithRequest:request delegate:self startImmediately:startImmediately];
}

@end


using namespace Cedar::Matchers;
using namespace Cedar::Doubles;


SPEC_BEGIN(NSURLConnectionSpec_Spec)

describe(@"NSURLConnection (spec extensions)", ^{
    __block NSURLRequest *request;
    __block NSURLConnection *connection;
    __block id<NSURLConnectionDelegate, CedarDouble> delegate;

    beforeEach(^{
        delegate = nice_fake_for(@protocol(NSURLConnectionDataDelegate));
        NSURL *url = [NSURL URLWithString:@"http://example.com"];
        request = [NSURLRequest requestWithURL:url];

        // OS X 10.7 introduced the NSURLDownload class, which has an initWithRequest:delegate: initializer
        // exactly like the one on NSURLConnection, EXCEPT that the type of the delegete argument is
        // id<NSURLDownloadDelegate> while the type of the NSURLConnection delegate argument is id.  The
        // compiler sees these as ambiguous methods with different arguments, so emits a warning.  Explicitly
        // cast the result of the alloc to NSURLConnection * to quiet the compiler.
        connection = [[(NSURLConnection *)[NSURLConnection alloc] initWithRequest:request delegate:delegate] autorelease];
    });

    describe(@"+resetAll", ^{
        it(@"should remove all connections from the global list of connections", ^{
            expect([NSURLConnection connections]).to_not(be_empty());
            [NSURLConnection resetAll];
            expect([NSURLConnection connections]).to(be_empty());
        });
    });

    describe(@"on initialization", ^{
        it(@"should record the existence of the object in the global list of connections", ^{
            expect([NSURLConnection connections]).to(contain(connection));
        });

        it(@"should retain the request", ^{
            expect(request.retainCount).to(equal(2));
        });

        describe(@"when the delegate is self", ^{
            beforeEach(^{
                connection = [[[SelfReferentialConnection alloc] initWithRequest:request delegate:nil startImmediately:YES] autorelease];
            });

            it(@"should not retain the delegate", ^{
                // One retain for alloc, one retain for the connections array.
                expect(connection.retainCount).to(equal(2));
            });
        });

        describe(@"when the delegate is not self", ^{
            __block ConnectionDelegate *delegate;

            beforeEach(^{
                delegate = [[[ConnectionDelegate alloc] init] autorelease];
                connection = [[(NSURLConnection *)[NSURLConnection alloc] initWithRequest:request delegate:delegate] autorelease];
            });

            it(@"should retain the delegate", ^{
                expect([delegate retainCount]).to(equal(2));
            });
        });
    });

    describe(@"on destruction", ^{
        beforeEach(^{
            request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
            delegate = nice_fake_for(@protocol(NSURLConnectionDataDelegate));

            connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
            [connection release]; connection = nil;
            [NSURLConnection resetAll];
        });

        it(@"should release the request", ^{
            expect(request.retainCount).to(equal(1));
        });

        describe(@"when the delegate is self", ^{
            beforeEach(^{
                connection = [[SelfReferentialConnection alloc] initWithRequest:request delegate:nil startImmediately:YES];
                [NSURLConnection resetAll];
            });

            it(@"should not explode", ^{
                [connection release]; connection = nil;
            });
        });

        describe(@"when the delegate is not self", ^{
            it(@"should release the delegate", ^{
                expect([delegate retainCount]).to(equal(1));
            });
        });
    });

    describe(@"when the connection is generated with the asynchronous convenience class method", ^{
        __block NSURL *URL;
        __block NSHTTPURLResponse *receivedResponse;
        __block NSData *receivedData;
        __block NSError *receivedError;

        beforeEach(^{
            receivedResponse = nil;
            receivedData = nil;
            receivedError = nil;

            URL = [NSURL URLWithString:@"http://www.google.com/"];
            NSURLRequest *request = [NSURLRequest requestWithURL:URL];
            [NSURLConnection sendAsynchronousRequest:request
                                               queue:[NSOperationQueue mainQueue]
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                       receivedData = data;
                                       receivedError = error;
                                       receivedResponse = (NSHTTPURLResponse *)response;
                                   }];
            connection = [[NSURLConnection connections] lastObject];
        });

        it(@"should be captured and made available in the array of connections", ^{
            connection.request.URL should equal(URL);
        });

        it(@"should receive responses", ^{
            PSHKFakeHTTPURLResponse *response = [[[PSHKFakeHTTPURLResponse alloc] initWithStatusCode:200 andHeaders:nil andBody:@"Response"] autorelease];

            [connection receiveResponse:response];

            NSString *receivedString = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];
            receivedString should equal(@"Response");
            receivedResponse.statusCode should equal(200);
        });

        it(@"should receive failures", ^{
            NSData *data = [@"Fail" dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = [[[NSError alloc] init] autorelease];

            [connection failWithError:error data:data];

            receivedData should be_nil; //SDK docs say that data is guaranteed to be nil when an error occurs
            receivedError should equal(error);
        });
    });
{'fraction_non_alphanumeric': 0.10378188214599825, 'fraction_numerical': 0.0023453532688361184, 'mean_word_length': 2.5313146997929605, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2976588', 'n_tokens_mistral': 3424, 'n_tokens_neox': 3137, 'n_words': 872}